Name

DataRelation

Synopsis

A DataRelation represents a parent-child relation between two tables. A DataSet stores DataRelation objects in its DataRelationCollection object. These DataRelation objects must be created programmatically, even if they are defined in the data source. ADO.NET data providers automatically determine relationships from the data source.

There are two reasons to add a DataRelation object. The first is make navigation easier. Once you have created the relation, you can use methods such as DataRow.GetChildRows( ) and DataRow.GetParentRow( ) to retrieve related rows. The second reason is to enforce relational constraints. When you create a relation, a corresponding ForeignKeyConstraint is generated by default and added to the table. As long as DataSet.EnforceConstraints is set to true, an exception is thrown when you attempt to perform an action that violates the relationship (such as adding a child that refers to a nonexistent parent).

The easiest way to create a DataRelation is to use the constructor that requires the relation name, the parent DataColumn, and the child DataColumn. Alternatively, you can relate multiple columns or use an overloaded version that allows you to prevent the creation of the ForeignKeyConstraint.

public class DataRelation {

// Public Constructors

   public DataRelation(string relationName, 

     DataColumn[ ] parentColumns, DataColumn[ ] childColumns);

   public DataRelation(string relationName,  DataColumn[ ] parentColumns, 

      DataColumn[ ] childColumns ...

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.