| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | ms.assetid | caps.latest.revision | author | ms.author | manager | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Adding DataRelations |
03/30/2017 |
.net-framework |
|
article |
a4a564fb-c1c4-4135-b6c2-b030e51195e4 |
4 |
JennieHubbard |
jhubbard |
jhubbard |
Adding DataRelations
In a xref:System.Data.DataSet with multiple xref:System.Data.DataTable objects, you can use xref:System.Data.DataRelation objects to relate one table to another, to navigate through the tables, and to return child or parent rows from a related table.
The arguments required to create a DataRelation are a name for the DataRelation being created, and an array of one or more xref:System.Data.DataColumn references to the columns that serve as the parent and child columns in the relationship. After you have created a DataRelation, you can use it to navigate between tables and to retrieve values.
Adding a DataRelation to a xref:System.Data.DataSet adds, by default, a xref:System.Data.UniqueConstraint to the parent table and a xref:System.Data.ForeignKeyConstraint to the child table. For more information about these default constraints, see DataTable Constraints.
The following code example creates a DataRelation using two xref:System.Data.DataTable objects in a xref:System.Data.DataSet. Each xref:System.Data.DataTable contains a column named CustID, which serves as a link between the two xref:System.Data.DataTable objects. The example adds a single DataRelation to the Relations collection of the xref:System.Data.DataSet. The first argument in the example specifies the name of the DataRelation being created. The second argument sets the parent DataColumn and the third argument sets the child DataColumn.
customerOrders.Relations.Add("CustOrders", _
customerOrders.Tables("Customers").Columns("CustID"), _
customerOrders.Tables("Orders").Columns("CustID")) customerOrders.Relations.Add("CustOrders",
customerOrders.Tables["Customers"].Columns["CustID"],
customerOrders.Tables["Orders"].Columns["CustID"]); A DataRelation also has a Nested property which, when set to true, causes the rows from the child table to be nested within the associated row from the parent table when written as XML elements using xref:System.Data.DataSet.WriteXml%2A . For more information, see Using XML in a DataSet.
See Also
DataSets, DataTables, and DataViews
ADO.NET Managed Providers and DataSet Developer Center