歸納ADO.NET 2.0新特性好處
關注ADO.NET的朋友一定知道ADO.NET 2.0新特性,昨天在圖書館看到一本關于簡介新特性的書,在這里把我的感受分析給大家聽聽。在這篇文章里我將盡量簡單的描述下ADO.NET 2.0的新特性,尤其是配合SQL Server 2005所展現(xiàn)出來的強大實力。如果想進一步了解ADO.NET 2.0編程方面的話,可以去閱讀Glenn Johnson的--"ADO.NET 2.0高級編程。
一:ADO.NET 2.0新特性功能強大的
#T#2005年底(2005年10月)與 SQL Server 2005一起出現(xiàn)的是 .NET Framework 2.0 版本,其中用來訪問數(shù)據(jù)庫的 ADO.NET類也升級到 ADO.NET 2.0 版。ADO.NET 2.0 除了增強舊功能外,也提供了相當多的新功能,包含了以基礎類為本(base-class-based)的數(shù)據(jù)源提供程序(provider)模型、異步訪問架構、批處理更新與大量數(shù)據(jù)復制(bulk copy)、SQL Server 2005 的回調(diào)通知、單一連接同時多執(zhí)行結果集(MARS)、執(zhí)行統(tǒng)計、強化的 DataSet 類等等。換句話說,若要有效發(fā)揮 SQL Server 2005 的功能,前端應用程序最好用 ADO.NET 2.0 來開發(fā)。
ADO.NET 2.0的新特性提供了相當多的新增功能,一些與數(shù)據(jù)源提供程序無關,也就是訪問各種數(shù)據(jù)庫都可以用到的功能,但有很大的一部分是專屬于 SQL Server 2005,針對 SQL Server 2005 的新功能提供給前端應用程序開發(fā)使用。
二: ADO.NET 2.0新特性使用多數(shù)據(jù)結果集(僅限2005)
在之前版本的 SQL Server 同一時間一條連接只能傳遞一個 SELECT 語法執(zhí)行后返回的結果集。如果想在一次連接后返回多個查詢內(nèi)容只能使用類似如下的方法來實現(xiàn):
- SqlDataAdapter myDataAdapter = new SqlDataAdapter("StoredProcedureName",myConnection);
 - myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
 - myDataAdapter.SelectCommand.Parameters.Add("@sqlstr",sqlstr);
 - DataSet ds = new DataSet();
 - myDataAdapter.Fill(ds);
 - return ds; ds.Tables[0],ds.Tables[1],ds.Tables[2],//分別對應三個結果集
 
SQL Server 2005提供了在同一條連接上可以同時傳遞多個沒有游標結構(cursorless)的結果集(也稱為默認結果集),此功能稱為 Multiple Active Resultsets(MARS)。如此可以節(jié)省需要同時打開的連接數(shù),但要注意的是連接字符串設置要加上 MultipleAct-iveResultSets=true 屬性,否則默認不啟動多數(shù)據(jù)結果集的功能。
- string connstr = "server=(local);
 - database=northwind;integrated security=true;";
 - SqlConnection conn = new SqlConnection(connstr);
 - conn.Open(); SqlCommand cmd1 = new SqlCommand("select * from customers", conn);
 - SqlCommand cmd2 = new SqlCommand("select * from orders", conn);
 - SqlDataReader rdr1 = cmd1.ExecuteReader();
 - // next statement causes an error prior to SQL Server 2005 SqlDataReader rdr2 = cmd2.ExecuteReader();
 - // now you can reader from rdr1 and rdr2 at the same time.
 
 















 
 
 
 
 
 
 