How to Create Data Relations?

Answer

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)

All ado.net Questions

Ask your interview questions on ado-net

Write Your comment or Questions if you want the answers on ado-net from ado-net Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---