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.
Constraints are not enforced unless the EnforceConstraints property is set to true.
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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.