Name

Relations

Synopsis


DataRelationCollection drc = DataSet.Relations;

Accesses the DataRelationCollection contained in a DataSet that contains the child DataRelation objects belonging to the DataSet. These objects relate tables in the DataSet using their primary and foreign keys and allow navigation between parent and child tables in both directions.

Examples

The DataRelationCollection has two methods that are used to add relations to the DataSet. The first is the Add( ) method, which in its simplest form takes a single DataRelation argument as shown in the following example:

DataRelation dr = new DataRelation("MyDataRelation",

    parentTable.Columns["PrimaryKeyField"],

    childTable.Columns["ForeignKeyField"]);

ds.Relations.Add(dr);

The other overloaded versions of the Add( ) method allow a DataRelation object to be created and added to the DataSet in a single statement, as shown in the following example:

DataTable dt1;

DataColumn col1, col2;

// ... code to define columns col1 and col2 and add them to the table dt1



DataTable dt2;

DataColumn col3, col4;

// ... code to define columns col3 and col4 and add them to the table dt2



DataSet ds = new DataSet();

ds.Tables.Add(dt1);

ds.Tables.Add(dt2);



// add a relation to the DataRelationCollection called MyRelation

// and create the ForeignKey constraint between parent col1 and child col3

ds.Relations.Add("MyRelation", col1, col3, true);



// add the relation between parent columns col1 and col2 and child columns

// col3 and col4. Do not create the ForeignKey constraint.

ds.Relations.Add("MyRelation", new DataColumn[] {col1, col2},

    new DataColumn[] {col3, col4}, false);

More than one DataRelation can be added to a DataSet in a single statement using the AddRange( ) method. This method adds DataRelation objects from a DataRelation array to the end of the DataRelationCollection, as the following example illustrates:

// code to create DataRelations dr1 and dr2

ds.Relations.AddRange(new DataRelation[] {dr1, dr2});

The IndexOf( ) method allows the index of an existing relation to be retrieved from the DataRelationCollection. This method has two overloads that allow the relation to be located using either the relation name or a reference to the relation, as shown in the following examples:

Int32 index = ds.Relations.IndexOf("MyDataRelation");

Int32 index = ds.Relations.IndexOf(dr);

The value returned by the IndexOf( ) method is zero-based. A value of -1 is returned if the specified relation object doesn’t exist within the collection.

There are also two methods that can remove a relation from the DataRelationCollection. The Remove( ) method removes a relation matching the relation name argument. The RemoveAt( ) method removes the relation at a specified index from the DataRelationCollection. The following examples illustrate both methods:

// remove the relation names MyDataRelation from the 

// DataRelationCollection for the DataSet 

ds.Relations.Remove("MyDataRelation");



// remove the first relation in the DataRelationCollection for the DataSet

ds.Relations.RemoveAt(0);

Finally, the Clear( ) method removes all the relations from the DataSet, as shown in the following example:

ds.Relations.Clear();