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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access