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 ...
Get ADO.NET in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.