骑牛上北京

 

用DataReader读取数据

 1 using System.Data;
 2 using System.Data.SqlClient;
 3 
 4  static void Main(string[] args)
 5         {
 6             SqlConnection thisConn = new SqlConnection(@"Server=.\Nie;uid=sa;pwd=111;Database=MyNorthwind");
 7              //@符号表示一个字符串字面量,它使此名称中的把斜线发挥了作用,否则就需要双反斜线(\\)将反斜线字符转义到C#字符串中
 8             thisConn.Open();
 9             SqlCommand thisCommand = thisConn.CreateCommand();
10             thisCommand.CommandText = "Select CustomerID,CustomerName from Customers";
11 
12 
13             SqlDataReader thisReader = thisCommand.ExecuteReader();
14             while (thisReader.Read())
15             {
16                 Console.WriteLine("\t(0)\t(1)",
17                     thisReader["CustomerID"], thisReader["CustomerName"]);
18             }
19             thisReader.Close();
20             thisConn.Close();
21             Console.Write("Program finished,press any key to Continue.");
22             Console.ReadLine(); 
24 
25         }
26   

 

posted on 2014-03-02 21:36  29882942  阅读(125)  评论(0编辑  收藏  举报

导航