|
|
In chapter 2, "Introduction to Databases," you saw how database tables can be related to each other through foreign keys. For example, the OrderID column of the Orders table is related to the CustomerID column of the Customers table through a foreign key. Because the Orders table depends on the Customers table, the Orders table is known as the child table and the Customers table as the parent table. The foreign key is said to define a parent-child relationship between the two tables.
Similarly, you can relate parent and child DataTable objects using a ForeignKeyConstraint or a DataRelation object. By default, when you add a DataRelation object to a DataSet, it actually adds a UniqueConstraint object to your parent DataTable along with a ForeignKeyConstraint to your child DataTable. The ForeignKeyConstraint ensures that each DataRow in your child DataTable has a matching DataRow in your parent DataTable.
In this chapter, you'll delve into the details of UniqueConstraint, ForeignKeyConstraint, and DataRelation objects. You'll also see how to navigate rows in related DataTable objects, make changes in related DataTable objects, and finally push those changes to the database.
Tip | To improve performance when loading a DataTable with large numbers of DataRow objects, you should set the EnforceConstraints property of your DataSet to false for the duration of the load. This stops the constraints from being enforced. Remember to set EnforceConstraints back to the default of true at the end of the load. |
Featured in this chapter:
The UniqueConstraint class
Creating a UniqueConstraint object
The ForeignKeyConstraint class
Creating a ForeignKeyConstraint object
The DataRelation class
Creating and using a DataRelation object
Adding, updating, and deleting related rows
Issues when updating the primary key of a parent row
Nested XML
Defining a relationship using Visual Studio .NET
|
|