DekGenius.com
[ Team LiB ] Previous Section Next Section

27.2 Properties Reference

ChildKeyConstraint

ForeignKeyConstraint fc = DataRelation.ForeignKeyConstraint;

Retrieves the ForeignKeyConstraint object that is associated with this relationship, if it exists. This constraint is applied to one or more DataColumns in the child table.

Example

The following code retrieves the associated ForeignKeyConstraint and uses it to ensure that cascading deletes are configured for this relationship. This means that a delete operation that affects the parent row automatically removes all related child rows as well.

ForeignKeyConstraint fk = dr.ChildKeyConstraint;
fk.DeleteRule = Rule.Cascade;
ChildTable

DataTable dt = DataRelation.ChildTable;

Retrieves the DataTable object for the child table in the relationship. For example, in a Customer figs/U2192.gif Orders relationship, this is the Orders table.

Example

The following code retrieves the child DataTable and displays some basic information about it in a console window:

DataTable dt = dr.ChildTable;
// Print the name and number of rows of the child table.
Console.WriteLine(dt.TableName, dt.Rows.Count.ToString());
DataSet

DataSet ds = DataRelation.DataSet;

Retrieves the DataSet that the DataRelation belongs to.

Example

The following code retrieves the appropriate DataSet and displays some basic information about it in a console window:

DataSet ds = dr.DataSet;

// Print the name and number of tables of the DataSet.
Console.WriteLine(ds.DataSetName, ds.Tables.Count.ToString());
Nested

Boolean nested = DataRelation.Nested;
DataRelation.Nested = nested;

Determines whether the XML output for this DataSet uses nesting for this relationship. If true, the XML elements that represent child rows appear inside the XML element that represents the corresponding parent row. For more information on XML and nesting rows, refer to Chapter 17.

Example

The following example displays the XML for a DataSet, both with and without nesting:

DataColumn parentCol = ds.Tables["Categories"].Columns["CategoryID"];
DataColumn childCol = ds.Tables["Products"].Columns["CategoryID"];
DataRelation dr = new DataRelation("Cat_Prod", parentCol, childCol);

ds.Relations.Add(dr);

// Write output without nesting.
ds.WriteXml(Console.Out);

// Writer output with nesting.
dr.Nested = true;
ds.WriteXml(Console.Out);

Without nesting, the XML output has this structure:

<?xml version="1.0" standalone="yes"?>
<Northwind>

  <Categories />
  <Categories />
   ...

  <Products />
  <Products />
   ...

</Northwind>

With nesting, the XML output has this form:

<?xml version="1.0" standalone="yes"?>
<Northwind>

  <Categories>
    <Products />
  </Categories>

  <Categories>
    <Products />
  </Categories>

   ...

</Northwind>

For more information about the XML representation of a DataSet, refer to Chapter 17.

Note

You can't nest more than one level deep because it can introduce row duplication. Thus, you can't nest the multiple levels of a many-to-many relationship.

ParentKeyConstraint

UniqueConstraint uc = DataRelation.ParentKeyConstraint;

Retrieves the UniqueKeyConstraint object that is associated with this relationship, if it exists. This constraint is applied to the DataColumn in the parent table.

Example

The following code retrieves the associated UniqueKeyConstraint and displays some basic information about it in a console window:

UniqueConstraint uc = dr.ParentKeyConstraint;

// Does this constraint represent a primary key?
Console.WriteLine(uc.IsPrimaryKey.ToString());
ParentTable

DataTable dt = DataRelation.ParentTable;

Retrieves the DataTable object for the parent table in the relationship. For example, in a Customer figs/U2192.gif Orders relationship, this is the Customers table. This property is primarily included for convenience; you can also retrieve the table directly from the DataSet.

Example

The following code retrieves the parent DataTable and displays some basic information about it in a console window:

DataTable dt = dr.ParentTable;

// Print the name and number of rows of the child table.
Console.WriteLine(dt.TableName, dt.Rows.Count.ToString());
RelationName

string relationName = DataRelation.RelationName;
DataRelation.RelationName = relationName;

This is the name of the DataRelation. It's primarily used to retrieve or remove a DataRelation object by name from the DataRelationCollection.

Example

The following code snippet retrieves a relation by its RelationName:

DataRelation dr = ds.Relations["Cat_Prod"];

Note

Often, the RelationName incorporates the parent and child column names, as in Customers_Orders. This convention isn't required, however.

    [ Team LiB ] Previous Section Next Section