Adding and Removing Relations

Relations belonging to the DataSet are stored as DataRelation objects in a DataRelationCollection object and are accessed through the Relations property of the DataSet. Each DataRelation object represents the relationship between a parent and child table in the DataSet. This section examines some methods and properties of the DataRelationCollection .

Relations are added to the DataSet using the Add( ) method of the DataRelationCollection, as shown in the following example:

ds.Relations.Add("MyDataRelation", parentTable.Columns["PrimaryKeyField"],

    childTable.Columns["ForeignKeyField"]);

The Remove( ) method removes a relation matching the relation-name argument. The following example removes the relation added in the previous example:

ds.Relations.Remove("MyDataRelation");

The Contains( ) method can determine if a specific relation exists as shown in the following example:

Boolean exists = ds.Relations.Contains("MyRelation");

Relations and the DataRelationCollection are discussed in detail in Chapter 11.

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.