[翻译]LINQ Project: Unified Language Features for Object and Relational Queries(4)

Exercise 3 – LINQ to DataSet: LINQ for DataSet Objects

This exercise demonstrates that the same features available for querying in-memory collections and querying xml can be applied to datasets.

本练习演示,相同的功能可用于查询在内存中的集合和查询 xml 也可应用于数据集。

Strongly typed dataSets (a key element of the ADO.NET programming model) provide a strongly typed representation of a set of tables and relationships that is disconnected from a database.  These datasets have the ability to cache data from a database and allow applications to manipulate the data in memory while retaining its relational shape. 

强类型 dataSets ADO.NET 编程模型的一个关键要素) 提供一组与数据库断开连接的强类型化表示形式的表和关系。  这些数据集可缓存数据库中数据的能力并允许应用程序来处理内存中的数据,同时保留其关系形状。

LINQ to DataSet provides the same rich query capabilities shown in LINQ to Objects and LINQ to XML.  In this task you explore how to load in datasets and create basic queries over the dataset.

DataSet LINQ 提供和对象的LINQ XMLLINQ 相同的丰富查询功能。  此任务中您了解如何加载数据集和通过数据集创建基本的查询。

Task 1 – Creating a DataSet – Add Designer File

While creating datasets is not new to LINQ, these first few tasks lead you through creation of a dataset to set up a framework in which to demonstrate LINQ to DataSet.

相对于创建数据集时LINQ并不陌生,这里首先有几个任务引导您完成创建数据集来设置要在其中演示 DataSet LINQ 一个框架。

1.  Create the DataSet item.  Right-click the LINQ Overview project and then click Add | New Item.

2.  In Templates, select DataSet.

3.   Provide a name for the new item by entering “NorthwindDS” in the Name box.

4.  Click OK.

Task 2 – Creating a DataSet – Add Data Connection

1.  In Microsoft Visual Studio, click the View | Server Explorer menu command (or press Ctrl+W,L).

2.  In the Server Explorer, click the Connect to database button.

3.  In the Add Connection dialog box, provide the local database server by entering “.\sqlexpress” in the Server name field.

4.  Select the database by choosing “Northwind” in the Select or enter a database name box.

5.  Click OK.

Task 3 – Creating a DataSet – Using the Designer

1.  In Server Explorer, expand Data Connections.

2.  Open the Northwind folder.

3.  Open the Tables folder.

4.  Open the NorthwindDS.xsd file by double clicking it from the Solution Explorer.

5.  From the tables folder drag the Customers table into the method pane.

1.  Press Ctrl+Shift+B to build the application. 

Task 4 – Querying a DataSet

1.  Return to the CreateCustomer method and update it to read data in from the Northwind DataSet created by the designer (notice the return type also has to change):

返回 CreateCustomer 方法并通过设计器从Northwind DataSet读取数据更新(请注意返回类型还会一直更改)

static NorthwindDS.CustomersDataTable CreateCustomers()

{

    SqlDataAdapter adapter = new SqlDataAdapter(

        "select * from customers",

        @"Data Source=.\sqlexpress;Initial Catalog=Northwind;" +

            "Integrated Security=true");      

 

    NorthwindDS.CustomersDataTable table =

        new NorthwindDS.CustomersDataTable();

 

    adapter.Fill(table);

 

    return table;

}  

 

2.  Return to the ObjectQuery method.  You will need to make one update to the print statement:

 

static void ObjectQuery()

{

    var results = from c in LoadCustomerTable()

                  where c.City == "London"

                        select c;

          foreach (var c in results)

        Console.WriteLine("{0}\t{1}", c.CustomerID, c.City);

}

 

Move the mouse over c to notice that the objects returned from the query and iterated over in the foreach loop are no longer Customers, rather they are CustomerRows.  These objects are defined in the DataSet and are a strongly typed view of the Customer Table in the Northwind Database. 

将鼠标移到c 上,注意,从该查询中返回和通过 foreach 循环访问中的对象将不再是Customers,而是 CustomerRows  这些对象定义在该 DataSet 中,是一个强类型的Northwind 数据库中Customer表的视图。

3.  Press Ctrl+F5 to build and run the application.  Notice the output still only contains those customers that are located in London.  Press any key to terminate the application.

 

posted @ 2007-11-20 16:56  cspfeng  阅读(321)  评论(0编辑  收藏  举报
测试