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.49028c.com/redcar/archive/2006/11/23/569859.html
新聞熱點
疑難解答