c__数据库连接和操作大全_第1页
c__数据库连接和操作大全_第2页
c__数据库连接和操作大全_第3页
c__数据库连接和操作大全_第4页
c__数据库连接和操作大全_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c# 数据库连接和操作大全一:数据库连接代码:SqlConnection objSqlConnection = new SqlConnection ("server = ;uid = sa; pwd =;database =test");objSqlConnection.Open();二:数据库的添加记录代码:            int i = 0;       &

2、#160;  string s1 = "", s2 = ""          i = Convert.ToInt16(textBox1.Text);              s1 = textBox2.Text;       

3、   s2 = textBox3.Text;            SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test");            objSq

4、lConnection.Open();                        MessageBox.Show("数据库连接成功", "好");              

5、try                                 SqlCommand sqlcom = new SqlCommand("insert into info(id,name,sex) values( " + i + ",'

6、" + s1 + "','" + s2 + "')", objSqlConnection);                  sqlcom.ExecuteNonQuery();              

7、     MessageBox.Show("添加成功!", "啊");                             catch (Exception a)      &

8、#160;                           MessageBox.Show(a.ToString();                    

9、;       MessageBox.Show("添加成功!", "啊");三:数据库的修改代码:            int i = 0;            string s1 = "", s2 = ""

10、;            s1 = textBox2.Text;            s2 = textBox3.Text;            if (textBox1.Text.Length = 0)  

11、0;             i = 0;            else                i = Convert.ToInt32(textBox1.Text); &

12、#160;          SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test");            objSqlConnection.Open();    &

13、#160;       MessageBox.Show("数据库连接成功", "好");            try                     

14、       SqlCommand sqlcom = new SqlCommand("update info set name='"+s1+"',sex='"+s2+"'"+"where id=" + i, objSqlConnection);              

15、60; sqlcom.ExecuteNonQuery();                MessageBox.Show("修改成功!", "啊");                objSqlConnection.Close()

16、;                        catch (Exception a)                      

17、0;     MessageBox.Show(a.ToString();            四:数据库的删除代码:            int i = 0;            st

18、ring s1 = "", s2 = ""            s1 = textBox2.Text;            s2 = textBox3.Text;            if(te

19、xtBox1.Text.Length=0)                i=0;            else                 i = Co

20、nvert.ToInt16(textBox1.Text);            SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test");              &

21、#160;         objSqlConnection.Open();            MessageBox.Show("数据库连接成功", "好");            try   

22、                         SqlCommand sqlcom = new SqlCommand("delete from info where id="+i, objSqlConnection);         &

23、#160;      sqlcom.ExecuteNonQuery();                MessageBox.Show("删除成功!", "啊");              

24、0; objSqlConnection.Close();                        catch (Exception a)                 &#

25、160;          MessageBox.Show(a.ToString();            五:数据库的查询代码:            1.类开始:       

26、0;          DataTable dt1 = new DataTable();                  SqlDataAdapter da1 = new SqlDataAdapter();         &#

27、160;  2.按钮代码:            int i = 0,n=0;            string s1 = "", s2 = ""            s1 =

28、 textBox2.Text;            s2 = textBox3.Text;           if (textBox1.Text.Length = 0)                i = 0;&#

29、160;           else                i = Convert.ToInt32(textBox1.Text);            SqlConnection objSqlCon

30、nection = new SqlConnection("server = ;uid = sa; pwd =;database =test");            objSqlConnection.Open();            MessageBox.Show("数据库连接成功", &quo

31、t;好");            string query = "SELECT * from info where id="+i;            DataSet objDataSet = new DataSet();        

32、0;   SqlDataAdapter obj = new SqlDataAdapter();                       obj.SelectCommand = new SqlCommand(query, objSqlConnection);       

33、60;    obj.Fill(objDataSet, "info");                     SqlCommand objSqlCommand = new SqlCommand(query, objSqlConnection);       

34、0;    SqlDataReader objSqlReader = objSqlCommand.ExecuteReader();             while (objSqlReader.Read()                   &#

35、160;                n += 1;                    MessageBox.Show("编号: " + objSqlReader.Getvalue(0) + " 

36、; 姓名:" + objSqlReader.Getvalue(1) + "  性别" + objSqlReader.Getvalue(2);                                if (n =

37、0)                    MessageBox.Show("数据库中没有这样的记录!");六:数据库的查询代码:       int i = 0;           / int n = 0; 

38、;           string s1 = "", s2 = ""            string sql;            s1 = textBox2.Text;  

39、0;         s2 = textBox3.Text;            if (textBox1.Text.Length = 0)                    

40、0;       i = 0;                                        else 

41、               i = Convert.ToInt32(textBox1.Text);           SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test");

42、                      objSqlConnection.Open();            MessageBox.Show("数据库连接成功", "好");   &#

43、160;        string query = "SELECT * from info where id="+i;            if(i=0)                  sql

44、 = "select * from info "            else                  sql = "select * from info where id=" + i;     &

45、#160;      da1 = new SqlDataAdapter(sql, objSqlConnection);            dt1.Clear();            da1.Fill(dt1);       

46、;     dataGridView1.DataSource = dt1;数据库的封装类代码:class DBClass            public void dbclass(string  sql)                 

47、;   try                            SqlConnection sqlcon = new SqlConnection("server = ;uid = sa; pwd =;database =test"); 

48、               sqlcon.Open();                             SqlTransaction objt = sqlco

49、n.BeginTransaction();  /事物开始                              SqlCommand sqlcom = new SqlCommand(sql, sqlcon);     

50、0;                          sqlcom.Transaction = objt; /将Command 对象设置为事物处理的对象               

51、60;            sqlcom.ExecuteNonQuery();                objt.Commit();         /提交事物                sqlcon.Close();                 

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论