
Creating Child DataView Objects
Thông tin tài liệu:
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ìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Creating Child DataView ObjectsTài liệu có liên quan:
-
52 trang 464 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 364 0 0 -
96 trang 333 0 0
-
74 trang 329 0 0
-
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 318 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 317 1 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 313 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 306 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 303 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 299 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 292 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 292 0 0 -
64 trang 290 0 0
-
Bài giảng An toàn và bảo mật thông tin - Trường đại học Thương Mại
31 trang 271 0 0 -
47 trang 261 0 0
-
LUẬN VĂN: TÌM HIỂU PHƯƠNG PHÁP HỌC TÍCH CỰC VÀ ỨNG DỤNG CHO BÀI TOÁN LỌC THƯ RÁC
65 trang 260 0 0 -
Giáo trình Hệ điều hành: Phần 2
53 trang 254 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 246 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 245 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 241 0 0