C#链接数据库的三部曲

 1  static void Main(string[] args)
 2         {
 3             int resLine = 0;
 4             //连接字符串
 5             string temp = @"ZZ-NBPC0003\AMSSQLSERVER";
 6             string str = "Data Source=" + temp + ";Initial Catalog=dbtest;Integrated Security=True;";
 7             using (SqlConnection sqlConn = new SqlConnection(str))      //1.建立连接
 8             {
 9                 //string sql = "select * from test update test set cTitle='数学' where CID BETWEEN 17 AND 20";
10                 string sql = "select * from test update test set cTitle='英语' where CID<5";
11                 string sql1 = "select COUNT(*) from test";
12                 string sql2 = "select * from test";
13                 using (SqlCommand cmd = new SqlCommand(sql, sqlConn))  //2.创建Command对象
14                 {
15 
16                     sqlConn.Open();                                    //3.打开并执行
17 
18                     resLine = cmd.ExecuteNonQuery();
19                     //SqlDataReader srd = cmd.ExecuteReader();   //insert/delete/update
20 
21                     //cmd.ExecuteReader();// 查询多行多列的时候    默认情况下独占一个sqlConn连接对象
22 
23                     //cmd.ExecuteScalar();//返回单个结果
24 
25                     //cmd.Dispose();
26 
27                 }

 

上一篇:MySQL实现学生表,成绩表,课程表,老师表复杂查询,用到了join等高级特性


下一篇:wordpress无插件纯代码调用最新文章