|
|
The ForeignKeyConstraint constructor is overloaded as follows:
ForeignKeyConstraint(DataColumn parentDataColumn, DataColumn childDataColumn) ForeignKeyConstraint(DataColumn[] parentDataColumns, DataColumn[] childDataColumns) ForeignKeyConstraint(string constraintName, DataColumn parentDataColumn, DataColumn childDataColumn) ForeignKeyConstraint(string constraintName, DataColumn[] parentDataColumns, DataColumn[] childDataColumns) ForeignKeyConstraint(string constraintName, string parentDataTableName, string[] parentDataColumnNames, string[] childDataColumnNames, AcceptRejectRule acceptRejectRule, Rule deleteRule, Rule updateRule)
where
parentDataColumn and parentDataColumns are the DataColumn objects in the parent DataTable.
childDataColumn and childDataColumns are the DataColumn objects in the child DataTable.
constraintName is the name you want to assign to the ConstraintName property of your ForeignKeyConstraint.
parentDataTableName is the name of the parent DataTable.
parentDataColumnNames and childDataColumnNames contain the names of the DataColumn objects in the parent and child DataTable objects.
acceptRejectRule, deleteRule, and updateRule are the various rules for the ForeignKey-Constraint.
Earlier in the section "Creating a UniqueConstraint Object," you saw a code example that created two DataTable objects named customersDT and ordersDT. The following example creates a ForeignKeyConstraint object on the CustomerID DataColumn of ordersDT to the CustomerID DataColumn of customersDT:
ForeignKeyConstraint myFKC = new ForeignKeyConstraint( "ForeignKeyConstraintCustomersOrders", customersDT.Columns["CustomerID"], ordersDT.Columns["CustomerID"] ); ordersDT.Constraints.Add(myFKC);
Notice that the ForeignKeyConstraint is added to ordersDT using the Add() method.
Note | To successfully add a ForeignKeyConstraint to a DataTable, each DataColumn value in the child DataTable must have a matching DataColumn value in the parent DataTable. |
The next example retrieves the constraint just added to ordersDT and displays its properties:
myFKC = (ForeignKeyConstraint) ordersDT.Constraints["ForeignKeyConstraintCustomersOrders"]; Console.WriteLine("myFKC.AcceptRejectRule = " + myFKC.AcceptRejectRule); Console.WriteLine("Columns:"); foreach (DataColumn myDataColumn in myFKC.Columns) { Console.WriteLine("" + myDataColumn); } Console.WriteLine("myFKC.ConstraintName = " + myFKC.ConstraintName); Console.WriteLine("myFKC.DeleteRule = " + myFKC.DeleteRule); Console.WriteLine("RelatedColumns:"); foreach (DataColumn relatedDataColumn in myFKC.RelatedColumns) { Console.WriteLine(""+ relatedDataColumn); } Console.WriteLine("myFKC.RelatedTable = " + myFKC.RelatedTable); Console.WriteLine("myFKC.Table = " + myFKC.Table); Console.WriteLine("myFKC.UpdateRule = " + myFKC.UpdateRule);
This example displays the following output:
myFKC.AcceptRejectRule = None Columns: CustomerID myFKC.ConstraintName = ForeignKeyConstraintCustomersOrders myFKC.DeleteRule = Cascade RelatedColumns: CustomerID myFKC.RelatedTable = Customers myFKC.Table = Orders myFKC.UpdateRule = Cascade
Note | You'll find all the code examples shown in this section and the previous section, "Creating a UniqueConstraint Object," in the AddConstraints.cs program. The listing is omitted from this book for brevity. |
|
|