Relationships in datasets
Collapse the table of content
Expand the table of content

Relationships in datasets


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.

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.

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.

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



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


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.


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


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


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 back to the database.

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.

When you create data tables with the data design tools in Visual Studio, relationships are created automatically if the information can be gathered from the source of your data. If you manually add data tables from the DataSet tab of the Toolbox, you may have to create the relationship manually. For information on creating DataRelation objects programmatically, see Adding DataRelations.

Relationships between DataTables appear as lines on the Dataset Designer with a key and infinity glyph depicting the one-to-many aspect of the relationship. By default the name of the relationship does not appear on the design surface.


Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the Visual Studio IDE.

To create a relationship between two DataTables

  1. Open your dataset in the Dataset Designer. For more information, see How to: Open a Dataset in the Dataset Designer.

  2. Drag a Relation object from the DataSet toolbox onto the child data table in the relationship.

    The Relation dialog box opens, populating the Child Table box with the table you dragged the Relation onto.

  3. Select the parent table from the Parent Table box. The parent table contains records on the "one" side of a one-to-many relationship.

  4. Verify the correct child table is displayed in the Child Table box. The child table contains records on the "many" side of a one-to-many relationship.

  5. Type a name for the relationship in the Name box, or leave the default name based on the selected tables. This is the name of the actual DataRelation object in code.

  6. Select the columns that join the tables in the Key Columns and Foreign Key Columns lists.

  7. Select whether to create a relation, constraint, or both. For information, see Introduction to DataRelation Objects.

  8. Select or clear the Nested Relation box. Selecting this option sets the Nested property to true, and it causes the child rows of the relation to be nested within the parent column when written as XML data or synchronized with an XmlDataDocument. For more information, see Nesting DataRelations.

  9. Set the rules to be enforced when making changes to records in these tables. For more information, see Rule.

  10. Click OK to create the relationship; a relation line appears on the designer between the two tables. You can toggle showing the relation name on the design surface by selecting or clearing Show Relation Labels on the Data menu.

To toggle the display of relation names in the Dataset Designer

  1. Open your dataset in the Dataset Designer. For more information, see How to: Open a Dataset in the Dataset Designer.

  2. From the Data menu, select or clear the Show Relation Labels command to toggle displaying the relation name on or off.

© 2016 Microsoft