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                 }

 

C#链接数据库的三部曲

上一篇:路子有点野--数据库telent+ip+1521连接不上,telnet+127.0.0.1+1521就可以连接上解决方法


下一篇:SQL Server Error: Saving Changes is not permitted. The changes you have made require the following tables to be dropped and recreated