ForeignKeyConstraint Class
Represents an action restriction enforced on a set of columns in a primary key/foreign key relationship when a value or row is either deleted or updated.
Assembly: System.Data (in System.Data.dll)
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.
The child rows can also be deleted (a cascading action).
The values in the child column (or columns) can be set to null values.
The values in the child column (or columns) can be set to default values.
An exception can be generated.
ForeignKeyConstraint objects are contained in the ConstraintCollection 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.
The following example creates a ForeignKeyConstraint, sets some of its properties, and adds it to a DataTable object's ConstraintCollection.
' The next line goes into the Declarations section of the module: ' SuppliersProducts is a class derived from DataSet. Private suppliersProducts As SuppliersProducts Private Sub CreateConstraint() ' Declare parent column and child column variables. Dim parentColumn As DataColumn Dim childColumn As DataColumn Dim fkeyConstraint As ForeignKeyConstraint ' Set parent and child column variables. parentColumn = suppliersProducts.Tables("Suppliers").Columns("SupplierID") childColumn = suppliersProducts.Tables("Products").Columns("SupplierID") fkeyConstraint = New ForeignKeyConstraint( _ "SupplierFKConstraint", parentColumn, childColumn) ' Set null values when a value is deleted. fkeyConstraint.DeleteRule = Rule.SetNull fkeyConstraint.UpdateRule = Rule.Cascade fkeyConstraint.AcceptRejectRule = AcceptRejectRule.Cascade ' Add the constraint, and set EnforceConstraints to true. suppliersProducts.Tables("Products").Constraints.Add(fkeyConstraint) suppliersProducts.EnforceConstraints = True End Sub
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.