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

  • 来源: 互联网 作者: rocket   2008-03-17/14:45
  • 觉得很多人在写关于ASP.NET2.0的东东,很少有人写关于ADO.NET2.0的新特性。查找了一下MSDN,给大家介绍几点好了。(如果需要察看所有ADO.NET2.0的新特性,请查看

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

     

    Server Enumeration

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

     

    可以参考以下示例代码:


    using System.Data.Sql;

    class Program
    {
      static void Main()
      {
        // Retrieve the enumerator instance and then the data.
        SqlDataSourceEnumerator instance =
          SqlDataSourceEnumerator.Instance;
        System.Data.DataTable table = instance.GetDataSources();

        // Display the contents of the table.
        DisplayData(table);

        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
      }

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


    DataSet Enhancements

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

    示例:


    private static void TestCreateDataReader(DataTable dt)
    {
        // Given a DataTable, retrieve a DataTableReader
        // allowing access to all the tables' data:
        using (DataTableReader reader = dt.CreateDataReader())
        {
            do
            {
                if (!reader.HasRows)
                {
                    Console.WriteLine("Empty DataTableReader");
                }
                else
                {
                    PrintColumns(reader);
                }
                Console.WriteLine("========================");
            } while (reader.NextResult());
        }
    }

    private static DataTable GetCustomers()
    {
        // Create sample Customers table, in order
        // to demonstrate the behavior of the DataTableReader.
        DataTable table = new DataTable();

        // Create two columns, ID and Name.
        DataColumn idColumn = table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name", typeof(string));

        // Set the ID column as the primary key column.
        table.PrimaryKey = new DataColumn[] { idColumn };

        table.Rows.Add(new object[] { 1, "Mary" });
        table.Rows.Add(new object[] { 2, "Andy" });
        table.Rows.Add(new object[] { 3, "Peter" });
        table.Rows.Add(new object[] { 4, "Russ" });
        return table;
    }

    private static void PrintColumns(DataTableReader reader)
    {
        // Loop through all the rows in the DataTableReader
        while (reader.Read())
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                Console.Write(reader[i] + " ");
            }
            Console.WriteLine();
        }
    }

     

     

    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

    DataTables跟DataSets现在提供一个新的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 SQL跟LINQ( 这东西现在出现频率还挺高的哦,也挺好玩的一个东东)查询EDM schemas
    .....
    LINQ(AUGUST CTP):

    LINQ to Entities: 使用LINQ查询EDM schemas
    LINQ to DataSet: 对一个或者多个DataTable进行LINQ查询
    都是很期待的技术,Enjoy it!:)


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多