随笔-1  评论-1  文章-1  trackbacks-0

ADO.NET中的sql连接

using System.Data;            // Use ADO.NET namespace
using System.Data.SqlClient; 

 SqlConnection thisConnection = new SqlConnection(
            @"Data Source=GY; Initial Catalog=northwind;uid=sa;password=datadog"); //先建立连接
 thisConnection.Open();//打开连接
         SqlCommand thisCommand = thisConnection.CreateCommand();//直接指定conn的command
也可以这样        //  SqlCommand cmd = new SqlCommand();
       //  cmd.Connection = thisConnection;
cmmand的sql命令        
thisCommand.CommandText = "SELECT CustomerID, CompanyName from Customer";
         SqlDataReader thisReader = thisCommand.ExecuteReader();//sqldatareader不可以用new 必须直接与command关联
//用reader读出表
         while (thisReader.Read())
         {
            // Output ID and name columns
            Console.WriteLine("\t{0}\t{1}",
               thisReader["CustomerID"], thisReader["CompanyName"]);
         // Close reader 用完要关闭
         thisReader.Close();

         // Close connection
         thisConnection.Close ()

posted on 2006-11-23 14:46 風清雲淡 阅读(694) 评论(1) 编辑 收藏

评论:
#1楼 2006-11-23 20:32 | 虾米      
加油~~~
 回复 引用 查看   
昵称:風清雲淡
园龄:5年2个月
粉丝:0
关注:0
<2006年11月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

搜索

 
 

常用链接

随笔档案

最新评论