Represents an action restriction enforced on a set of columns in a child/parent relationship when a value or row is either deleted or updated.
Object
Constraint
ForeignKeyConstraint
[Visual Basic] Public Class ForeignKeyConstraint Inherits Constraint [C#] public class ForeignKeyConstraint : Constraint [C++] public __gc class ForeignKeyConstraint : public Constraint [JScript] public class ForeignKeyConstraint extends Constraint
A ForeignKeyConstraint restricts the action performed when a value in a column (or columns) is either deleted or updated. Such a constraint is intended to be used with primary key columns. In a parent/child relationship between two tables, deleting a value from the parent table can affect the child rows in one of the following ways.
ForeignKeyConstraint objects are contained in the ConstraintsCollection of a DataTable, which is accessed through the Constraints property.
Constraints are not enforced unless the EnforceConstraints property is set to true.
The AcceptRejectRule is enforced whenever a DataTable object's AcceptChanges method is invoked.
Namespace: System.Data
Assembly: System.Data.dll
The following example creates a ForeignKeyConstraint, sets various of its properties, and adds it to a DataTable object's ConstraintsCollection.
[Visual Basic]
' The next line goes into the Declarations section of the module: ' SuppliersProducts is a class derived from DataSet. Private myDataSet As SuppliersProducts Private Sub CreateConstraint() ' Declare parent column and child column variables. Dim pCol As DataColumn Dim cCol As DataColumn Dim myFKC As ForeignKeyConstraint ' Set parent and child column variables. pCol = MyDataSet.Tables("Suppliers").Columns("SupplierID") cCol = MyDataSet.Tables("Products").Columns("SupplieriD") myFKC = New ForeignKeyConstraint("SuppierFKConstraint", pCol, cCol) ' Set null values when a value is deleted. myFKC.DeleteRule = Rule.SetNull myFKC.UpdateRule = Rule.Cascade myFKC.AcceptRejectRule = AcceptRejectRule.Cascade ' Add the constraint, and set EnforceConstraints to true. myDataSet.Tables("Suppliers").Constraints.Add myFKC myDataSet.EnforceConstraints = True End Sub
ForeignKeyConstraint Members | System.Data Namespace | AcceptRejectRule | DataTable | ConstraintsCollection | Rule