Introduction to DataRelation Objects

Datasets that contain related data tables use DataRelation objects to represent a parent/child relationship between the tables and to return related records from one another. Adding related tables to datasets using the Data Source Configuration Wizard, or the Dataset Designer, creates and configures the DataRelation object for you. For more information on returning related records, see How to: Access Records in Related DataTables. For information on creating data relations, see How to: Create DataRelations with the Dataset Designer.

The DataRelation object performs two functions:

  • It can make available the records related to a record you are working with. It provides child records if you are in a parent record (GetChildRows) and a parent record if you are working with a child record (GetParentRow ).

  • It can enforce constraints for referential integrity, such as deleting related child records when you delete a parent record.

It is important to understand the difference between a true join and the function of a DataRelation object. In a true join, records are taken from parent and child tables and put into a single, flat recordset. When you use a DataRelation object, no new recordset is created. Instead, the relation tracks the relationship between tables and keeps parent and child records in synch.

DataRelation Objects and Constraints

A DataRelation object is also used to create and enforce the following constraints:

  • A unique constraint, which guarantees that a column in the table contains no duplicates.

  • A foreign-key constraint, which can be used to maintain referential integrity between a parent and child table in a dataset.

Constraints that you specify in a DataRelation object are implemented by automatically creating appropriate objects or setting properties. If you create a foreign-key constraint using the DataRelation object, instances of the ForeignKeyConstraint class are added to the DataRelation's ChildKeyConstraint property.

A unique constraint is implemented either by simply setting the Unique property of a data column to true or by adding an instance of the UniqueConstraint class to the DataRelation object's ParentKeyConstraint. For information on suspending constraints in a dataset, see How to: Turn Off Constraints While Filling a Dataset.

Referential Integrity Rules

As part of the foreign-key constraint, you can specify referential integrity rules that are applied at three points:

  • When a parent record is updated

  • When a parent record is deleted

  • When a change is accepted or rejected

The rules you can make are specified in the Rule enumeration and are listed in the following table.

Foreign-key constraint rule

Action

Cascade

The change (update or delete) made to the parent record is made in related records in the child table as well.

SetNull

Child records are not deleted, but the foreign key in the child records is set to DBNull. With this setting, child records can be left as "orphans" — that is, they have no relationship to parent records.

Note

Using this rule can result in invalid data in the child table.

SetDefault

The foreign key in the related child records is set to its default value (as established by the column's DefaultValue property).

None

No change is made to related child records. With this setting, child records can end up containing references to invalid parent records.

For more information about updates in dataset tables, see Saving Data in Datasets.

Constraint-only Relations

When creating a DataRelation object, you have the option of specifying that the relation be used only to enforce constraints — that is, it will not also be used to access related records. This option allows you to generate a dataset that is slightly more efficient and that contains fewer methods than one with the related-records capability. However, you will not be able to access related records. For example, a constraint-only relation prevents you from deleting a parent record that still has children, and you cannot access the child records through the parent.

See Also

Reference

DataRelation

GetChildRows

GetParentRows

Data Source Configuration Wizard

Concepts

Working with Datasets in Visual Studio

Preparing Your Application to Receive Data

Editing Data in Your Application

Creating and Editing Typed Datasets