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.
public ref class ForeignKeyConstraint : System::Data::Constraint
public class ForeignKeyConstraint : System.Data.Constraint
[System.Serializable]
public class ForeignKeyConstraint : System.Data.Constraint
type ForeignKeyConstraint = class
inherit Constraint
[<System.Serializable>]
type ForeignKeyConstraint = class
inherit Constraint
Public Class ForeignKeyConstraint
Inherits Constraint
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
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.
Constructors ForeignKeyConstraint(DataColumn, DataColumn)Initializes a new instance of the ForeignKeyConstraint class with the specified parent and child DataColumn objects.
ForeignKeyConstraint(DataColumn[], DataColumn[])Initializes a new instance of the ForeignKeyConstraint class with the specified arrays of parent and child DataColumn objects.
ForeignKeyConstraint(String, DataColumn, DataColumn)Initializes a new instance of the ForeignKeyConstraint class with the specified name, parent and child DataColumn objects.
ForeignKeyConstraint(String, DataColumn[], DataColumn[])Initializes a new instance of the ForeignKeyConstraint class with the specified name, and arrays of parent and child DataColumn objects.
ForeignKeyConstraint(String, String, String, String[], String[], AcceptRejectRule, Rule, Rule)This constructor is provided for design time support in the Visual Studio environment. ForeignKeyConstraint objects created by using this constructor must then be added to the collection via AddRange(Constraint[]). Tables and columns with the specified names must exist at the time the method is called, or if BeginInit() has been called prior to calling this constructor, the tables and columns with the specified names must exist at the time that EndInit() is called.
ForeignKeyConstraint(String, String, String[], String[], AcceptRejectRule, Rule, Rule)This constructor is provided for design time support in the Visual Studio environment. ForeignKeyConstraint objects created by using this constructor must then be added to the collection via AddRange(Constraint[]). Tables and columns with the specified names must exist at the time the method is called, or if BeginInit() has been called prior to calling this constructor, the tables and columns with the specified names must exist at the time that EndInit() is called.
Properties Methods Thread SafetyThis type is safe for multithreaded read operations. You must synchronize any write operations.
See alsoCollaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. In this articleWas this page helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4