다음을 통해 공유


Navigating a Relationship between Tables

One of the primary functions of a DataRelation is to allow navigation from one DataTable to another within a DataSet. This allows you to retrieve all the related DataRow objects in one DataTable when given a single DataRow from a related DataTable. For example, after establishing a DataRelation between a table of customers and a table of orders, you can retrieve all the order rows for a particular customer row using DataRow.GetChildRows.

The following code example creates a DataRelation between the Customers table and the Orders table of a DataSet and returns all the orders for each customer.

Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _
                     custDS.Tables("Customers").Columns("CustomerID"), _
                     custDS.Tables("Orders").Columns("CustomerID")) 

Dim custRow As DataRow
Dim orderRow As DataRow

For Each custRow in custDS.Tables("Customers").Rows
  Console.WriteLine(custRow("CustomerID"))
  For Each orderRow in custRow.GetChildRows(custOrderRel)
    Console.WriteLine(orderRow("OrderID"))
  Next
Next
[C#]
DataRelation custOrderRel = custDS.Relations.Add("CustOrders",
                     custDS.Tables["Customers"].Columns["CustomerID"],
                     custDS.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow custRow in custDS.Tables["Customers"].Rows)
{
  Console.WriteLine(custRow["CustomerID"]);
  foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel))
    Console.WriteLine(orderRow["OrderID"]);
}

The next example builds on the preceding example, relating four tables together and navigating those relationships. As in the previous example, CustomerID relates the Customers table to the Orders table. For each customer in the Customers table, all the child rows in the Orders table are determined, in order to return the number of orders a particular customer has and their OrderID values.

The expanded example also returns the values from the OrderDetails and Products tables. The Orders table is related to the OrderDetails table using OrderID to determine, for each customer order, what products and quantities were ordered. Because the OrderDetails table only contains the ProductID of an ordered product, OrderDetails is related to Products using ProductID in order to return the ProductName. In this relation, the Products table is the parent and the Order Details table is the child. As a result, when iterating through the OrderDetails table, GetParentRow is called to retrieve the related ProductName value.

Notice that when the DataRelation is created for the Customers and Orders tables, no value is specified for the createConstraints flag (the default is true). This assumes that all the rows in the Orders table have a CustomerID value that exists in the parent Customers table. If a CustomerID exists in the Orders table that does not exist in the Customers table, a ForeignKeyConstraint will cause an exception to be thrown.

In situations where the child column might contain values that the parent column does not contain, set the createConstraints flag to false when adding the DataRelation. In the example, the createConstraints flag is set to false for the DataRelation between the Orders table and the OrderDetails table. This enables the application to return all the records from the OrderDetails table and only a subset of records from the Orders table without generating a run-time exception. The expanded sample generates output in the following format.

      Customer ID: NORTS
        Order ID: 10517
              Order Date: 4/24/1997 12:00:00 AM
                 Product: Filo Mix
                Quantity: 6
                 Product: Raclette Courdavault
                Quantity: 4
                 Product: Outback Lager
                Quantity: 6
        Order ID: 11057
              Order Date: 4/29/1998 12:00:00 AM
                 Product: Outback Lager
                Quantity: 3

The following code example is an expanded sample where the values from the OrderDetails and Products tables are returned, with only a subset of the records in the Orders table being returned.

Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _
                     custDS.Tables("Customers").Columns("CustomerID"), _
                     custDS.Tables("Orders").Columns("CustomerID"))

Dim orderDetailRel As DataRelation = custDS.Relations.Add("OrderDetail", _
                     custDS.Tables("Orders").Columns("OrderID"), _
                     custDS.Tables("OrderDetails").Columns("OrderID"), false)

Dim orderProductRel As DataRelation = custDS.Relations.Add("OrderProducts", _
                     custDS.Tables("Products").Columns("ProductID"), _
                     custDS.Tables("OrderDetails").Columns("ProductID"))

Dim custRow, orderRow, detailRow As DataRow

For Each custRow In custDS.Tables("Customers").Rows
  Console.WriteLine("Customer ID:" & custRow("CustomerID").ToString())

  For Each orderRow In custRow.GetChildRows(custOrderRel)
    Console.WriteLine("  Order ID: " & orderRow("OrderID").ToString())
    Console.WriteLine(vbTab & "Order Date: " & orderRow("OrderDate").ToString())

    For Each detailRow In orderRow.GetChildRows(orderDetailRel)
        Console.WriteLine(vbTab & "   Product: " & detailRow.GetParentRow(orderProductRel)("ProductName").ToString())
        Console.WriteLine(vbTab & "  Quantity: " & detailRow("Quantity").ToString())
    Next
  Next
Next
[C#]
DataRelation custOrderRel = custDS.Relations.Add("CustOrders",
                     custDS.Tables["Customers"].Columns["CustomerID"],
                     custDS.Tables["Orders"].Columns["CustomerID"]);

DataRelation orderDetailRel = custDS.Relations.Add("OrderDetail",
                     custDS.Tables["Orders"].Columns["OrderID"],
                     custDS.Tables["OrderDetails"].Columns["OrderID"], false);

DataRelation orderProductRel = custDS.Relations.Add("OrderProducts",
                     custDS.Tables["Products"].Columns["ProductID"],
                     custDS.Tables["OrderDetails"].Columns["ProductID"]);

foreach (DataRow custRow in custDS.Tables["Customers"].Rows)
{
  Console.WriteLine("Customer ID: " + custRow["CustomerID"]);

  foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel))
  {
    Console.WriteLine("  Order ID: " + orderRow["OrderID"]);
    Console.WriteLine("\tOrder Date: " + orderRow["OrderDate"]);

    foreach (DataRow detailRow in orderRow.GetChildRows(orderDetailRel))
    {
        Console.WriteLine("\t   Product: " + detailRow.GetParentRow(orderProductRel)["ProductName"]);
        Console.WriteLine("\t  Quantity: " + detailRow["Quantity"]);
    }
  }
}

See Also

Creating and Using DataSets | DataRelation Class | DataRow Class | DataSet Class