CS 112D Lecture Notes - DataSet Table Relations, Using DataSet Component (Chapter 5 part 2)
DataSet Table Relations and Foreign Key Constraints
- DataRelation - a class to store a parent/child relationship between two DataTables in a
DataSet.
- The parent table is the one with the primary key, and the child table is the one with
the foreign key. (e.g. Department is the parent of Employee.)
- DataRelation objects allow you to easily lookup related information in another table, using a
DataSet.
- DataRelation objects are stored in the Relations collection of the DataSet. When
constructing/adding them, specify the parent column followed by the child column.
- Example code can be seen in the btnCreateRelations_Click subroutine in the
The DataSets solution from the text book
- When you add a DataRelation to a DataSet, two Constraint objects are automatically added as well: a
ForeignKeyConstraint that enforces the referential integrity, and a UniqueConstraint that
ensures that the parent table's primary key is unique.
- If a ForeignKeyConstraint is violated, a Data.InvalidConstraintException is thrown.
Using the DataSet Component
- Most of the things we've done with DataSets in code could be done using the DataSet Component from
the Data tab of the Visual Studio Toolbox.
Return to the main CS 112D page