成功信息网数据库连接操作大全下面是c#与数据库的连接及增删改除的各种操作,全部经过上机验证。本人是从事软件开发,如果各位有什么问题或者需要帮助的可以在我的空间留言。一:数据库连接代码:SqlConnectionobjSqlConnection=newSqlConnection(server=127.0.0.1;uid=sa;pwd=;database=test);objSqlConnection.Open();二:数据库的添加记录代码:inti=0;strings1=,s2=;i=Convert.ToInt16(textBox1.Text);s1=textBox2.Text;s2=textBox3.Text;SqlConnectionobjSqlConnection=newSqlConnection(server=127.0.0.1;uid=sa;pwd=;database=test);objSqlConnection.Open();MessageBox.Show(数据库连接成功,好);try{SqlCommandsqlcom=newSqlCommand(insertintoinfo(id,name,sex)values(+i+,'+s1+','+s2+'),objSqlConnection);sqlcom.ExecuteNonQuery();MessageBox.Show(添加成功!,啊);}catch(Exceptiona){MessageBox.Show(a.ToString());}MessageBox.Show(添加成功!,啊);}三:数据库的修改代码:inti=0;strings1=,s2=;s1=textBox2.Text;s2=textBox3.Text;if(textBox1.Text.Length==0)i=0;elsei=Convert.ToInt32(textBox1.Text);成功信息网=newSqlConnection(server=127.0.0.1;uid=sa;pwd=;database=test);objSqlConnection.Open();MessageBox.Show(数据库连接成功,好);try{SqlCommandsqlcom=newSqlCommand(updateinfosetname='+s1+',sex='+s2+'+whereid=+i,objSqlConnection);sqlcom.ExecuteNonQuery();MessageBox.Show(修改成功!,啊);objSqlConnection.Close();}catch(Exceptiona){MessageBox.Show(a.ToString());}四:数据库的删除代码:inti=0;strings1=,s2=;s1=textBox2.Text;s2=textBox3.Text;if(textBox1.Text.Length==0)i=0;elsei=Convert.ToInt16(textBox1.Text);SqlConnectionobjSqlConnection=newSqlConnection(server=127.0.0.1;uid=sa;pwd=;database=test);objSqlConnection.Open();MessageBox.Show(数据库连接成功,好);try{SqlCommandsqlcom=newSqlCommand(deletefrominfowhereid=+i,objSqlConnection);sqlcom.ExecuteNonQuery();MessageBox.Show(删除成功!,啊);objSqlConnection.Close();}catch(Exceptiona){MessageBox.Show(a.ToString());}五:数据库的查询代码:1.类开始:DataTabledt1=newDataTable();成功信息网=newSqlDataAdapter();2.按钮代码:inti=0,n=0;strings1=,s2=;s1=textBox2.Text;s2=textBox3.Text;if(textBox1.Text.Length==0)i=0;elsei=Convert.ToInt32(textBox1.Text);SqlConnectionobjSqlConnection=newSqlConnection(server=127.0.0.1;uid=sa;pwd=;database=test);objSqlConnection.Open();MessageBox.Show(数据库连接成功,好);stringquery=SELECT*frominfowhereid=+i;DataSetobjDataSet=newDataSet();SqlDataAdapterobj=newSqlDataAdapter();obj.SelectCommand=newSqlCommand(query,objSqlConnection);obj.Fill(objDataSet,info);SqlCommandobjSqlCommand=newSqlCommand(query,objSqlConnection);SqlDataReaderobjSqlReader=objSqlCommand.ExecuteReader();while(objSqlReader.Read()){n+=1;MessageBox.Show(编号:+objSqlReader.Getvalue(0)+姓名:+objSqlReader.Getvalue(1)+性别+objSqlReader.Getvalue(2));}if(n==0)MessageBox.Show(数据库中没有这样的记录!);六:数据库的查询代码:inti=0;//intn=0;strings1=,s2=;stringsql;s1=textBox2.Text;s2=textBox3.Text;if(textBox1.Text.Length==0){i=0;}elsei=Convert.ToInt32(textBox1.Text);成功信息网=newSqlConnection(server=127.0.0.1;uid=sa;pwd=;database=test);objSqlConnection.Open();MessageBox.Show(数据库连接成功,好);stringquery=SELECT*frominfowhereid=+i;if(i==0)sql=select*frominfo;elsesql=select*frominfowhereid=+i;da1=newSqlDataAdapter(sql,objSqlConnection);dt1.Clear();da1.Fill(dt1);dataGridView1.DataSource=dt1;数据库的封装类代码:classDBClass{publicvoiddbclass(stringsql){try{SqlConnectionsqlcon=newSqlConnection(server=127.0.0.1;uid=sa;pwd=;database=test);sqlcon.Open();SqlTransactionobjt=sqlcon.BeginTransaction();//事物开始SqlCommandsqlcom=newSqlCommand(sql,sqlcon);sqlcom.Transaction=objt;//将Command对象设置为事物处理的对象sqlcom.ExecuteNonQuery();objt.Commit();//提交事物sqlcon.Close();}catch(Exceptiona){MessageBox.Show(a.ToString());}}}成功信息网数据库连接代码:stringstrcon=Provider=IBMDADB2;DataSource=hfzd;UserId=db2admin;Password=db2admin;//stringsql=select*fromADMINISTRATOR.HFZD;stringsql=deletefromADMINISTRATOR.HFZDwhereID=1;OleDbConnectionolecon=newOleDbConnection(strcon);olecon.Open();MessageBox.Show(数据库已连接上);dt.Clear();da=newOleDbDataAdapter(sql,olecon);da.Fill(dt);dataGridView1.DataSource=dt;olecon.Close();软件开发的路很辛苦,希望各位兄弟姐妹好好坚持下去,这是一条不错的路,相信自己。