WEB开发网
开发学院WEB开发ASP.NET ADO.NET中的sql连接 阅读

ADO.NET中的sql连接

 2006-11-24 17:48:51 来源:WEB开发网   
核心提示:using System.Data; // Use ADO.NET namespaceusing System.Data.SqlClient; SqlConnection thisConnection = new SqlConnection( @"Data Source=GY; Initial Cat

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 ()

http://www.cnblogs.com/redcar/archive/2006/11/23/569859.html

Tags:ADO NET sql

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接