首页 > 代码库 > C#数据库查询和操作大全

C#数据库查询和操作大全

一:C#数据库查询之数据库连接代码:

  1. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");  
  2. objSqlConnection.Open(); 

二:数据库的添加记录代码:

  1. inti=0;  
  2. strings1="",s2="";  
  3. i=Convert.ToInt16(textBox1.Text);  
  4. s1=textBox2.Text;  
  5. s2=textBox3.Text;  
  6.  
  7. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");  
  8. objSqlConnection.Open();  
  9.  
  10. MessageBox.Show("数据库连接成功","好");  
  11.  
  12. try  
  13. {  
  14. SqlCommandsqlcom=newSqlCommand("insertintoinfo(id,name,sex)values("+i+",‘"+s1+"‘,‘"+s2+"‘)",objSqlConnection);  
  15. sqlcom.ExecuteNonQuery();  
  16. MessageBox.Show("添加成功!","啊");  
  17. }  
  18. catch(Exceptiona)  
  19. {  
  20. MessageBox.Show(a.ToString());  
  21. }  
  22. MessageBox.Show("添加成功!","啊");  

三:数据库的修改代码:

  1. inti=0;  
  2. strings1="",s2="";  
  3. s1=textBox2.Text;  
  4. s2=textBox3.Text;  
  5. if(textBox1.Text.Length==0)  
  6. i=0;  
  7. else 
  8. i=Convert.ToInt32(textBox1.Text);  
  9.  
  10. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");  
  11. objSqlConnection.Open();  
  12. MessageBox.Show("数据库连接成功","好");  
  13. try  
  14. {  
  15. SqlCommandsqlcom=newSqlCommand("updateinfosetname=‘"+s1+"‘,sex=‘"+s2+"‘"+"whereid="+i,objSqlConnection);  
  16. sqlcom.ExecuteNonQuery();  
  17. MessageBox.Show("修改成功!","啊");  
  18. objSqlConnection.Close();  
  19. }  
  20. catch(Exceptiona)  
  21. {  
  22. MessageBox.Show(a.ToString());  

四:数据库的删除代码:

  1. inti=0;  
  2. strings1="",s2="";  
  3. s1=textBox2.Text;  
  4. s2=textBox3.Text;  
  5. if(textBox1.Text.Length==0)  
  6. i=0;  
  7. else 
  8. i=Convert.ToInt16(textBox1.Text);  
  9. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");  
  10. objSqlConnection.Open();  
  11. MessageBox.Show("数据库连接成功","好");  
  12. try 
  13. {  
  14. SqlCommandsqlcom=newSqlCommand("deletefrominfowhereid="+i,objSqlConnection);  
  15.  
  16. sqlcom.ExecuteNonQuery();  
  17.  
  18. MessageBox.Show("删除成功!","啊");  
  19.  
  20. objSqlConnection.Close();  
  21. }  
  22. catch(Exceptiona)  
  23. {  
  24. MessageBox.Show(a.ToString());  

五:数据库的查询代码:

1.类开始:

  1. DataTabledt1=newDataTable();  
  2. SqlDataAdapterda1=newSqlDataAdapter(); 

2.按钮代码:

  1. inti=0,n=0;  
  2. strings1="",s2="";  
  3. s1=textBox2.Text;  
  4. s2=textBox3.Text;  
  5. if(textBox1.Text.Length==0)  
  6. i=0;  
  7. else 
  8. i=Convert.ToInt32(textBox1.Text);  
  9. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");  
  10. objSqlConnection.Open();  
  11. MessageBox.Show("数据库连接成功","好");  
  12.  
  13. stringquery="SELECT*frominfowhereid="+i;  
  14.  
  15. DataSetobjDataSet=newDataSet();  
  16.  
  17. SqlDataAdapterobj=newSqlDataAdapter();  
  18.  
  19. obj.SelectCommand=newSqlCommand(query,objSqlConnection);  
  20.  
  21. obj.Fill(objDataSet,"info");  
  22.  
  23. SqlCommandobjSqlCommand=newSqlCommand(query,objSqlConnection);  
  24.  
  25. SqlDataReaderobjSqlReader=objSqlCommand.ExecuteReader();  
  26.  
  27. while(objSqlReader.Read())  
  28. {  
  29. n+=1;  
  30. MessageBox.Show("编号:"+objSqlReader.Getvalue(0)+"姓名:"+objSqlReader.Getvalue(1)+"性别"+objSqlReader.Getvalue(2));  
  31. }  
  32. if(n==0)  
  33. MessageBox.Show("数据库中没有这样的记录!"); 

六:C#数据库查询代码:

  1. inti=0;  
  2. //intn=0;  
  3. strings1="",s2="";  
  4. stringsql;  
  5. s1=textBox2.Text;  
  6. s2=textBox3.Text;  
  7.  
  8. if(textBox1.Text.Length==0)  
  9. {  
  10. i=0;  
  11.  
  12. }  
  13. else 
  14. i=Convert.ToInt32(textBox1.Text);  
  15. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");  
  16. objSqlConnection.Open();  
  17. MessageBox.Show("数据库连接成功","好");  
  18. stringquery="SELECT*frominfowhereid="+i;  
  19. if(i==0)  
  20. sql="select*frominfo";  
  21. else 
  22. sql="select*frominfowhereid="+i;  
  23. da1=newSqlDataAdapter(sql,objSqlConnection);  
  24. dt1.Clear();  
  25. da1.Fill(dt1);  
  26. dataGridView1.DataSource=dt1; 

C#数据库查询之数据库的封装类代码:

  1. classDBClass  
  2. {  
  3.  
  4. publicvoiddbclass(stringsql)  
  5. {  
  6. try 
  7. {  
  8. SqlConnectionsqlcon=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");  
  9. sqlcon.Open();  
  10.  
  11. SqlTransactionobjt=sqlcon.BeginTransaction();//事物开始  
  12.  
  13. SqlCommandsqlcom=newSqlCommand(sql,sqlcon);  
  14.  
  15. sqlcom.Transaction=objt;//将Command对象设置为事物处理的对象  
  16.  
  17. sqlcom.ExecuteNonQuery();  
  18. objt.Commit();//提交事物  
  19. sqlcon.Close();  
  20. }  
  21. catch(Exceptiona)  
  22. {  
  23. MessageBox.Show(a.ToString());  
  24. }  
  25.  
  26. }  
  27. }  
  28. --db2数据库连接代码:  
  29. stringstrcon="Provider=IBMDADB2;DataSource=hfzd;UserId=db2admin;Password=db2admin";  
  30. //stringsql="select*fromADMINISTRATOR.HFZD";  
  31. stringsql="deletefromADMINISTRATOR.HFZDwhereID=1";  
  32. OleDbConnectionolecon=newOleDbConnection(strcon);  
  33. olecon.Open();  
  34. MessageBox.Show("数据库已连接上");  
  35. dt.Clear();  
  36. da=newOleDbDataAdapter(sql,olecon);  
  37. da.Fill(dt);  
  38. dataGridView1.DataSource=dt;  
  39. olecon.Close();

C#数据库查询和操作大全