ADO.NET中的sql连接

王朝c#·作者佚名  2008-05-30
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航