Represents a parent/child relationship between two tables.
Object
DataRelation
[Visual Basic] Public Class DataRelation [C#] public class DataRelation [C++] public __gc class DataRelation [JScript] public class DataRelation
A DataRelation is used to relate two DataTable objects to each other through DataColumn objects. For example, in a Customer/Orders relationship, the Customers table is the parent and the Orders table is the child of the relationship.
Relationships are created between matching columns in the parent and child tables. That is, the DataType value for both columns must be identical.
Relationships can also cascade various changes to the parent row down to it's child rows. To control how values are changed in child rows, add a ForeignKeyConstraint to the DataTable class's ConstraintsCollection, which determines what happens when a value in a parent table is deleted or updated.
When a DataRelation is created, it verifies that the relationship can be established. Once it is added to the Relations collection, this is maintained by disallowing any changes that would invalidate this relation. However, before it is added to the Relations collection, this can change. So, every access of the object between construction and collection add verifies that the state is still valid, and throws an exception if it is no longer a viable relation.
DataRelation objects are contained in a RelationsCollection, and both the DataSet and the DataTable classes have properties to access the collection. the DataSet class's Relations property, and the DataTable class's ChildRelations and ParentRelations properties all access different RelationsCollection objects.
Namespace: System.Data
Assembly: System.Data.dll
The following example creates a new DataRelation and adds it to a DataSet object's RelationsCollection.
[Visual Basic]
Private Sub CreateRelation() ' Get the DataColumn objects from two DataTable objects in a DataSet. Dim parentCol As DataColumn Dim childCol As DataColumn ' Code to get the DataSet not shown here. parentCol = DataSet1.Tables("Customers").Columns("CustID") childCol = DataSet1.Tables("Orders").Columns("CustID") ' Create DataRelation. Dim relCustOrder As DataRelation CustOrderRel = New DataRelation("CustomersOrders", parentCol, childCol) ' Add the relation to the DataSet. DataSet1.Relations.Add(CustOrderRel) End Sub
DataRelation Members | System.Data Namespace | ChildRelations | DataColumn | DataSet | ForeignKeyConstraint | RelationsCollection | UniqueConstraint | ParentRelations