The DataRelation class, found within the System.Data namespace, makes table joins within a DataSet possible. Each relationship includes a parent and a child. The DataRelation class even uses the terms “parent” and “child” in its defining members.
To create a relationship between two DataSet tables, first add the parent and child table to the data set. Then create a new DataRelation instance, passing its constructor the name of the new relationship, plus a reference to the linking columns in each table. The following code joins a Customer table with an Order table, linking the Customer.ID column as the parent with the related Order.CustomerID column as the child:
C#
DataSet orderTracking = new DataSet("OrderTracking");
orderTracking.Tables.Add(customerTable);
orderTracking.Tables.Add(orderTable);
DataRelation customerOrder = new DataRelation("CustomerOrder", customerTable.Columns["ID"], orderTable.Columns["CustomerID"]);
orderTracking.Relations.Add(customerOrder);
Visual Basic
Dim orderTracking As New DataSet("OrderTracking")
orderTracking.Tables.Add(customerTable)
orderTracking.Tables.Add(orderTable)
Dim customerOrder As New DataRelation("CustomerOrder", customerTable.Columns!ID, orderTable.Columns!CustomerID) orderTracking.Relations.Add(customerOrder)