[ Team LiB ] |
Recipe 3.4 Navigating Between Parent and Child Records Using a DataRelationProblemYou want to navigate between the parent and child records in a hierarchical DataSet. SolutionUse a DataRelation to find the child rows for a parent row in a related table, or to find the parent row for a child row in a related table. The sample code starts by creating a DataSet containing the Orders and Order Details tables from the Northwind sample database and a relation between them. The code then iterates over the Orders and uses the relation to return all Order Detail rows for each order. Finally, the sample code retrieves the CustomerID field from the parent row and displays it for each child. The C# code is shown in Example 3-4. Example 3-4. File: NavigateDataRelationForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Text; using System.Data; using System.Data.SqlClient; // Table name constants private const String ORDERS_TABLE = "Orders"; private const String ORDERDETAILS_TABLE = "OrderDetails"; // Relation name constants private const String ORDERS_ORDERDETAILS_RELATION = "Orders_OrderDetails_Relation"; // Field name constants private const String ORDERID_FIELD = "OrderID"; private const String PRODUCTID_FIELD = "ProductID"; private const String QUANTITY_FIELD = "Quantity"; private const String CUSTOMERID_FIELD = "CustomerID"; // . . . DataSet ds = new DataSet( ); SqlDataAdapter da; // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM Orders", ConfigurationSettings.AppSettings["Sql_ConnectString"]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.Fill(orderTable); ds.Tables.Add(orderTable); // Fill the OrderDetails table and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM [Order Details]", ConfigurationSettings.AppSettings["Sql_ConnectString"]); DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE); da.Fill(orderDetailTable); ds.Tables.Add(orderDetailTable); // Create a relation between the tables. ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION, ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD], ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD], true); StringBuilder result = new StringBuilder( ); // Iterate over the first 10 order records. for (int i = 0; i < 10; i++) { // Display data from the Order record. result.Append("Order ID:" + orderTable.Rows[i][ORDERID_FIELD] + Environment.NewLine); // Iterate over the OrderDetails records for the Order. foreach(DataRow row in orderTable.Rows[i].GetChildRows(ORDERS_ORDERDETAILS_RELATION)) { // Display data for the OrderDetails record. result.Append("\tProduct: " + row[PRODUCTID_FIELD] + "; Quantity: " + row[QUANTITY_FIELD] + // Retrieve CustomerID from the parent Orders record. "; Customer ID: " + row.GetParentRow (ORDERS_ORDERDETAILS_RELATION)[CUSTOMERID_FIELD] + Environment.NewLine); } result.Append(Environment.NewLine); } resultTextBox.Text = result.ToString( ); DiscussionThe GetChildRows( ) method of a row in the parent table returns the child rows as an array of DataRow objects for a specified DataRelation. The method takes an optional second argument that you can use to specify the version of the data to retrieve as one of the DataRowVersion enumeration values: Current, Default, Original, or Proposed. Similarly, the GetParentRow( ) method of a row in the child table returns the parent row as a DataRow object for a specified DataRelation. Again, an optional second argument allows a specific version of the data to be returned. The GetParentRows( ) method can also be called on a row in the child table to return parent rows in situations where the child row can have multiple parents. This method returns an array of DataRow objects. Few commercial database products support many-to-many relationships between parent and child records. Many-to-many relationships are decomposed into two one-to-many relationships through an intermediate table in relational database management systems (RDBMS). |
[ Team LiB ] |