O'Reilly logo

ADO.NET in a Nutshell by Bill Hamilton, Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required