熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ADO.NET中的SQL連接方法代碼

2013-11-13 11:08:54  來源: .NET編程 

 
using SystemData; // Use ADONET namespace
using SystemDataSqlClient;

SqlConnection thisConnection = new SqlConnection(
@Data Source=GY; Initial Catalog=northwind;uid=sa;password=datadog); //先建立連接
thisConnectionOpen();//打開連接
SqlCommand thisCommand = thisConnectionCreateCommand();//直接指定conn的command
也可以這樣 // SqlCommand cmd = new SqlCommand();
// cmdConnection = thisConnection;
cmmand的sql命令
thisCommandCommandText = SELECT CustomerID CompanyName from Customer;
SqlDataReader thisReader = thisCommandExecuteReader();//sqldatareader不可以用new 必須直接與command關聯
//用reader讀出表
while (thisReaderRead())
{
// Output ID and name columns
ConsoleWriteLine(\t{}\t{}
thisReader[CustomerID] thisReader[CompanyName]);
// Close reader 用完要關閉
thisReaderClose();

// Close connection
thisConnectionClose ()


From:http://tw.wingwit.com/Article/program/net/201311/15259.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.