Many a time an application has to work with related data. A DataSet does not have a database's inherent capacity to maintain relations, so we have to create a DataRelation object that can maintain these relations.
If we continue with the Northwind example, a DataSet might have some data from the Customers table and the Orders table. Even if the tables contain a key in common (CustomerID), the DataSet itself does not keep track of the records in one table that relate to those in another. You can create a DataRelation object that references the parent and child tables (and their keys), and then use the object to work with the related tables.
The DataRelation object can perform two functions:
- It can provide either with the child record or with the parent record depending upon what we are working with.
- It can enforce constraints for referential integrity, such as deleting orphaned records.
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. A DataRelation object maintains the synchronization between the related tables and no new recordset is created. Instead, the relation tracks the relationship between tables and keeps parent and child records in synch.