System.Data Namespace
Represents a parent/child relationship between two DataTable objects.
Visibility |
Constructor |
Parameters |
public |
DataRelation |
(
String
relationName
,
DataColumn
parentColumn
,
DataColumn
childColumn
)
|
public |
DataRelation |
(
String
relationName
,
DataColumn
parentColumn
,
DataColumn
childColumn
,
Boolean
createConstraints
)
|
public |
DataRelation |
(
String
relationName
,
DataColumn
parentColumns
,
DataColumn
childColumns
)
|
public |
DataRelation |
(
String
relationName
,
DataColumn
parentColumns
,
DataColumn
childColumns
,
Boolean
createConstraints
)
|
public |
DataRelation |
(
String
relationName
,
String
parentTableName
,
String
childTableName
,
String
parentColumnNames
,
String
childColumnNames
,
Boolean
nested
)
|
public |
DataRelation |
(
String
relationName
,
String
parentTableName
,
String
parentTableNamespace
,
String
childTableName
,
String
childTableNamespace
,
String
parentColumnNames
,
String
childColumnNames
,
Boolean
nested
)
|
|
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. This is similar to a primary key/foreign key relationship. For more information, see Navigating a Relationship between Tables.
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 from the parent DataRow to its child rows. To control how values are changed in child rows, add a ForeignKeyConstraint to the ConstraintCollection of the DataTable object. The ConstraintCollection what action to take when a value in a parent table is deleted or updated.
When a DataRelation is created, it first verifies that the relationship can be established. After it is added to the DataRelationCollection, the relationship is maintained by disallowing any changes that would invalidate it. Between the period when a DataRelation is created and added to the DataRelationCollection, it is possible for additional changes to be made to the parent or child rows. An exception is generated if this results in a relationship that is no longer valid.
DataRelation objects are contained in a DataRelationCollection, which you can access through the Relations property of the DataSet, and the ChildRelations and ParentRelations properties of the DataTable.
The following example initializes a new DataRelation and adds it to the DataRelationCollection of a DataSet.
DataRelation categoryId = new DataRelation ( "categoryId",
myDataSet.Tables [ "Categories" ].Columns [ "CategoryID" ],
myDataSet.Tables [ "Products" ].Columns [ "CategoryID" ] );
// add the relation to the DataSet.
myDataSet.Relations.Add ( categoryId );
Dim categoryId As New DataRelation ( "categoryId", _
myDataSet.Tables ( "Categories" ).Columns ( "CategoryID" ), _
myDataSet.Tables ( "Products" ).Columns ( "CategoryID" ) )
' add the relation to the DataSet.
myDataSet.Relations.Add ( categoryId ) |
|
C# |
VB |
Show me
ChildRelations DataColumn DataSet ForeignKeyConstraint DataRelationCollection UniqueConstraint ParentRelations