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. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");
  7. objSqlConnection.Open();
  8. MessageBox.Show("数据库连接成功","好");
  9. try
  10. {
  11. SqlCommandsqlcom=newSqlCommand("insertintoinfo(id,name,sex)values("+i+",'"+s1+"','"+s2+"')",objSqlConnection);
  12. sqlcom.ExecuteNonQuery();
  13. MessageBox.Show("添加成功!","啊");
  14. }
  15. catch(Exceptiona)
  16. {
  17. MessageBox.Show(a.ToString());
  18. }
  19. MessageBox.Show("添加成功!","啊");
  20. }

三:数据库的修改代码:

 
 
 
  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. SqlConnectionobjSqlConnection=newSqlConnection("server=127.0.0.1;uid=sa;pwd=;database=test");
  10. objSqlConnection.Open();
  11. MessageBox.Show("数据库连接成功","好");
  12. try
  13. {
  14. SqlCommandsqlcom=newSqlCommand("updateinfosetname='"+s1+"',sex='"+s2+"'"+"whereid="+i,objSqlConnection);
  15. sqlcom.ExecuteNonQuery();
  16. MessageBox.Show("修改成功!","啊");
  17. objSqlConnection.Close();
  18. }
  19. catch(Exceptiona)
  20. {
  21. MessageBox.Show(a.ToString());
  22. }

四:数据库的删除代码:

 
 
 
  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. sqlcom.ExecuteNonQuery();
  16. MessageBox.Show("删除成功!","啊");
  17. objSqlConnection.Close();
  18. }
  19. catch(Exceptiona)
  20. {
  21. MessageBox.Show(a.ToString());
  22. }

五:数据库的查询代码:

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. stringquery="SELECT*frominfowhereid="+i;
  13. DataSetobjDataSet=newDataSet();
  14. SqlDataAdapterobj=newSqlDataAdapter();
  15. obj.SelectCommand=newSqlCommand(query,objSqlConnection);
  16. obj.Fill(objDataSet,"info");
  17. SqlCommandobjSqlCommand=newSqlCommand(query,objSqlConnection);
  18. SqlDataReaderobjSqlReader=objSqlCommand.ExecuteReader();
  19. while(objSqlReader.Read())
  20. {
  21. n+=1;
  22. MessageBox.Show("编号:"+objSqlReader.Getvalue(0)+"姓名:"+objSqlReader.Getvalue(1)+"性别"+objSqlReader.Getvalue(2));
  23. }
  24. if(n==0)
  25. MessageBox.Show("数据库中没有这样的记录!");

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

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

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

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

当前名称:C#数据库查询和操作大全
本文来源:http://www.csdahua.cn/qtweb/news29/457779.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网