/********************************************************************************* * * 功能描述: ADO.NET的基本化操做 * * 做 者: 郭強生 * * 修改日期: 2012-08-13 * * 備 注: 對北風數據庫進行相關的操做 ************************************************************************************/ namespace Statement { using System; using System.Data.SqlClient; class Program { static void Main(string[] args) { string strConn = "server=.;database=Northwind;uid=sa;pwd=0904031028"; //sqlConnection對象 SqlConnection conn = new SqlConnection(strConn); try { //打開數據庫鏈接 conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; //cmd.CommandType= cmd.CommandText = "select RegionID,RegionDescription from Region "; //獲取SqlDataReader實例 SqlDataReader reader = cmd.ExecuteReader(); //輸出結果 Console.WriteLine("{0}\t{1}", "RegionID"," RegionDescription"); Console.WriteLine(); while (reader.Read()) Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0), reader.GetString(1)); conn.Close(); } catch (Exception e) { Console.WriteLine("沒法鏈接到數據庫,報告異常:"); Console.WriteLine(e.Message); } } } }