tailieunhanh - Creating and Using a DataRelation Object

Creating and Using a DataRelation Object In this section, you'll learn how to create a DataRelation object to define a relationship between two DataTable objects that hold some rows from the Customers and Orders tables | Creating and Using a DataRelation Object In this section you ll learn how to create a DataRelation object to define a relationship between two DataTable objects that hold some rows from the Customers and Orders tables. As you know the CustomerlD column of the child Orders table is a foreign key that links to the CustomerlD column of the parent Customers table. Once you ve created a DataRelation you can use the GetChildRows method of a DataRow object in the parent DataTable to obtain the corresponding DataRow objects from the child DataTable. By corresponding I mean the rows that have matching values in the foreign key DataColumn objects. You can also use the GetParentRow method of a DataRow in the child DataTable to obtain the corresponding DataRow in the parent DataTable. Before creating and adding a DataRelation to a DataSet you first need a DataSet. The following example creates and populates a DataSet with two DataTable objects named customersDT and ordersDT notice that the top two rows from the Customers table along with the corresponding rows from the Orders table are retrieved SqlCommand mySqlCommand SELECT TOP 2 CustomerlD CompanyName FROM Customers ORDER BY CustomerlD SELECT OrderlD CustomerlD FROM Orders WHERE CustomerlD IN SELECT TOP 2 CustomerlD FROM Customers ORDER BY CustomerlD SqlDataAdapter mySqlDataAdapter new SqlDataAdapter mySqlCommand DataSet myDataSet new DataSet myDataSet Table .TableName Customers Table1 .TableName Orders DataTable customersDT Customers DataTable ordersDT Orders You ll see how to create a DataRelation that defines a relationship between the customersDT and ordersDT DataTable objects next. Note You ll find all the code examples shown in this section in the program. Creating the DataRelation

TÀI LIỆU LIÊN QUAN
10    158    1
6    184    1
7    162    1
5    157    1
6    160    1
6    152    1
6    150    1
6    206    1
7    154    1
5    121    1