ADO.NET2.0跟ADO.NET3.0的一些新特性简要介绍

觉得很多人在写关于ASP.NET2.0的东东,很少有人写关于ADO.NET2.0的新特性。查找了一下MSDN,给大家介绍几点好了。(如果需要察看所有ADO.NET2.0的新特性,请查看

http://msdn2.microsoft.com/en-us/library/ex6y04yf.aspx

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

Server Enumeration

用来枚举活动状态的SQL Server实例,版本需要在SQL2000及更新版本。使用的是SqlDataSourceEnumerator

 

可以参考以下示例代码:

None.gif using  System.Data.Sql;
None.gif
None.gif
class  Program
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
// Retrieve the enumerator instance and then the data.
InBlock.gif
    SqlDataSourceEnumerator instance =
InBlock.gif      SqlDataSourceEnumerator.Instance;
InBlock.gif    System.Data.DataTable table 
= instance.GetDataSources();
InBlock.gif
InBlock.gif    
// Display the contents of the table.
InBlock.gif
    DisplayData(table);
InBlock.gif
InBlock.gif    Console.WriteLine(
"Press any key to continue.");
InBlock.gif    Console.ReadKey();
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private static void DisplayData(System.Data.DataTable table)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
foreach (System.Data.DataRow row in table.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
foreach (System.Data.DataColumn col in table.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        Console.WriteLine(
"{0} = {1}", col.ColumnName, row[col]);
ExpandedSubBlockEnd.gif      }

InBlock.gif      Console.WriteLine(
"============================");
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

DataSet Enhancements

新的DataTableReader类可以说是一个DataSet或者DataTable,的一个或者多个的read-only, forward-only的结果集。需要说明的是,DataTable返回的DataTableReader不包含被标记为deleted的行。

示例:

None.gif private   static   void  TestCreateDataReader(DataTable dt)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// Given a DataTable, retrieve a DataTableReader
InBlock.gif    
// allowing access to all the tables' data:
InBlock.gif
    using (DataTableReader reader = dt.CreateDataReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
do
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!reader.HasRows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"Empty DataTableReader");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrintColumns(reader);
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.WriteLine(
"========================");
ExpandedSubBlockEnd.gif        }
 while (reader.NextResult());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
private   static  DataTable GetCustomers()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// Create sample Customers table, in order
InBlock.gif    
// to demonstrate the behavior of the DataTableReader.
InBlock.gif
    DataTable table = new DataTable();
InBlock.gif
InBlock.gif    
// Create two columns, ID and Name.
InBlock.gif
    DataColumn idColumn = table.Columns.Add("ID"typeof(int));
InBlock.gif    table.Columns.Add(
"Name"typeof(string));
InBlock.gif
InBlock.gif    
// Set the ID column as the primary key column.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    table.PrimaryKey = new DataColumn[] dot.gif{ idColumn };
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    table.Rows.Add(
new object[] dot.gif1"Mary" });
ExpandedSubBlockStart.gifContractedSubBlock.gif    table.Rows.Add(
new object[] dot.gif2"Andy" });
ExpandedSubBlockStart.gifContractedSubBlock.gif    table.Rows.Add(
new object[] dot.gif3"Peter" });
ExpandedSubBlockStart.gifContractedSubBlock.gif    table.Rows.Add(
new object[] dot.gif4"Russ" });
InBlock.gif    
return table;
ExpandedBlockEnd.gif}

None.gif
None.gif
private   static   void  PrintColumns(DataTableReader reader)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// Loop through all the rows in the DataTableReader
InBlock.gif
    while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for (int i = 0; i < reader.FieldCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.Write(reader[i] 
+ " ");
ExpandedSubBlockEnd.gif        }

InBlock.gif        Console.WriteLine();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif


Binary Serialization for the DataSet

关于这点linkcd已经写过一篇性能测试的文章:.Net 2.0 Data Container性能比较: Binary Serialize Dataset vs Custom Classes

 

DataTable as a Stand-Alone Object

很多以前DataSet的方法,现在可以用DataTable直接使用了

 

Create a DataTable from a DataView

现在可以从DataView返回一个DataTable了,两者基本是一样的,当然你也可以有选择性的返回,比如说返回distinct rows

 

New DataTable Loading Capabilities

DataTablesDataSets现在提供一个新的Load方法,可以直接把DataReader中的数据流载入到DataTable中,当然你也可以对如何Load做一些选择。

 

 

以上是ADO.NET2.0的一些特性,你使用.NET2.0进行开发,就可以使用这些特性。

更激动人心的在于
ADO.NET3.0的一些特性.

文章介绍了一些ADO.NET3.0 AUGUT CTP的一些特性:

The ADO.NET Entity Framework

    • The Entity Data Model (EDM),实体数据模型,开发者可以以更高的抽象层次来设计数据模型

    • 一个很牛的client-views/mapping引擎,用来映射(map to and form)存储结构(store schemas )

    • 完全支持使用Entity SQLLINQ( 这东西现在出现频率还挺高的哦,也挺好玩的一个东东)查询EDM schemas

    • .....

LINQAUGUST CTP:

    • LINQ to Entities: 使用LINQ查询EDM schemas

    • LINQ to DataSet: 对一个或者多个DataTable进行LINQ查询

都是很期待的技术,Enjoy it!:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值