NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Adding a Relationship Between Two Tables

Because the DataSet can contain multiple DataTable objects, there must also be a way to relate each table to another. This is needed for navigating through the tables, and for returning related values.

The essential arguments of a DataRelation are the two columns that serve as the primary key and the foreign key columns in the relation, and the name of the DataRelation. The name can then be used for navigating or when retrieving values.

Note   Relations can also be built with more than one column per table. You can use an array of DataColumn objects for both the primary and the foreign key arguments.

The example below presumes two DataTable objects exist in the DataSet. Both tables have a column named "CustID" which serves as the link between the two tables. The example adds a single DataRelation to the DataSet object's Relations collection. The first argument ("CustOrders") specifies the name of the relation. The second and third arguments are the DataColumn objects that link the two tables.

[VB]
Dim ds As DataSet = New DataSet("CustomerOrders")
' With ds do the following:
'  - add a customer table with CustID and CustName as columns.
'  - set Customers.CustID as the PrimaryKey column.
'  - add an Orders table with three columns: OrderID, CustID, and Quantity
'  - set Orders.OrderID as the PrimaryKey column.
' Add a DataRelation to the Relations collection specifying its name, and the 
' appropriate DataColumn objects as arguments.
ds.Relations.Add("CustOrders", ds.Tables("Customers").Columns("CustID"), _
         ds.Tables("Orders").Columns("CustID"))

' Or
Dim dr As DataRelation = _
New DataRelation("CustOrders", ds.Tables("Customers").Columns("CustID"), _
         ds.Tables("Orders").Columns("CustID"))
ds.Relations.Add(dr)

[C#]
   DataSet ds = new DataSet("CustomerOrders");

   // With ds do the following:
   // - add a customer table with CustID and CustName as columns
   // - set Customers.CustID as Primary Key
   // - add an Orders table with three columns: OrderID, CustID, and Quantity
   // - set Orders.OrderID as primary key
  // Add a DataRelation to the Relations collection specifying its name, 
  // and the appropriate DataColumn objects as arguments.

ds.Relations.Add("CustOrders",ds.Tables["Customers"].Columns["CustID"], 
     ds.Tables["Orders"].Columns["CustID"]);
// Or
DataRelation dr;
dr = new DataRelation("CustOrders",ds.Tables["Customers"].Columns["CustID"],
                        ds.Tables["Orders"].Columns["CustID"]);
ds.Relations.Add(dr)