Danh mục tài liệu

Creating Child DataView Objects

Số trang: 4      Loại file: pdf      Dung lượng: 15.89 KB      Lượt xem: 6      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Creating Child DataView Objects You can create a child DataView from a parent DataRowView using the CreateChildView() method. You can then view the DataRowView objects from the child DataView.
Nội dung trích xuất từ tài liệu:
Creating Child DataView ObjectsCreating Child DataView ObjectsYou can create a child DataView from a parent DataRowView using theCreateChildView() method. You can then view the DataRowView objects from the childDataView. To call the CreateChildView() method, you must first add a DataRelation tothe DataSet that defines a relationship between the two underlying DataTable objects.(See Chapter 12, Navigating and Modifying Related Data, for information aboutDataRelation objects.)Lets take a look at an example. Assume you have two DataTable objects namedcustomersDT and ordersDT. Also assume youve added the following DataRelation to theDataSet that defines a relationship between customersDT and ordersDT:DataRelation customersOrdersDataRel = new DataRelation( CustomersOrders, customersDT.Columns[CustomerID], ordersDT.Columns[CustomerID] );myDataSet.Relations.Add( customersOrdersDataRel);Finally, assume you have a DataView named customersDV that views the customers thathave a Country column of UK. You can then call the CreateChildView() method from aDataRowView in customersDV to create a child DataView; notice that the name of theDataRelation (CustomersOrders) is passed to the CreateChildView() method:DataView ordersDV = customersDV[0].CreateChildView(CustomersOrders);The ordersDV DataView allows you to access the child rows from the ordersDTDataTable. The parent in this example is the first DataRowView from customersDV witha CustomerID of AROUT. The child ordersDV DataView contains DataRowViewobjects with the details of the orders for the AROUT customer.Note The CreateChildView() method is overloaded. The other version of this method accepts a DataRelation object as the parameter.Listing 13.4 shows a complete example program.Listing 13.4: CREATECHILDDATAVIEW.CS/* CreateChildDataView.cs illustrates how to create a child DataView*/using System;using System.Data;using System.Data.SqlClient;class CreateChildDataView{ public static void Main() { SqlConnection mySqlConnection = new SqlConnection( server=localhost;database=Northwind;uid=sa;pwd=sa ); SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = SELECT CustomerID, CompanyName, Country + FROM Customers; + SELECT OrderID, CustomerID + FROM Orders;; SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); mySqlDataAdapter.SelectCommand = mySqlCommand; DataSet myDataSet = new DataSet(); mySqlConnection.Open(); mySqlDataAdapter.Fill(myDataSet); mySqlConnection.Close(); myDataSet.Tables[Table].TableName = Customers; myDataSet.Tables[Table1].TableName = Orders; DataTable customersDT = myDataSet.Tables[Customers]; DataTable ordersDT = myDataSet.Tables[Orders]; // add a DataRelation object to myDataSet DataRelation customersOrdersDataRel = new DataRelation( CustomersOrders, customersDT.Columns[CustomerID], ordersDT.Columns[CustomerID] ); myDataSet.Relations.Add( customersOrdersDataRel ); // create a DataView object named customersDV DataView customersDV = new DataView(); customersDV.Table = customersDT; customersDV.RowFilter = Country = UK; customersDV.Sort = CustomerID; // display the first row in the customersDV DataView object Console.WriteLine(Customer:); for (int count = 0; count < customersDV.Table.Columns.Count; count++) { Console.WriteLine(customersDV[0][count]); } // create a child DataView named ordersDV that views // the child rows for the first customer in customersDV DataView ordersDV = customersDV[0].CreateChildView(CustomersOrders); // display the child rows in the customersDV DataView object Console.WriteLine(\nOrderIDs of the orders placed by this customer:); foreach (DataRowView ordersDRV in ordersDV) { Console.WriteLine(ordersDRV[OrderID]); } }}The output from this program is as follows:Customer:AROUTAround the HornUKOrderIDs of the orders placed by this customer:10355103831045310558107071074110743107681079310864109201095311016

Tài liệu được xem nhiều:

Tài liệu có liên quan: