System.Data Namespace
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.
Visibility |
Constructor |
Parameters |
public |
ForeignKeyConstraint |
(
DataColumn
parentColumn
,
DataColumn
childColumn
)
|
public |
ForeignKeyConstraint |
(
String
constraintName
,
DataColumn
parentColumn
,
DataColumn
childColumn
)
|
public |
ForeignKeyConstraint |
(
DataColumn
parentColumns
,
DataColumn
childColumns
)
|
public |
ForeignKeyConstraint |
(
String
constraintName
,
DataColumn
parentColumns
,
DataColumn
childColumns
)
|
public |
ForeignKeyConstraint |
(
String
constraintName
,
String
parentTableName
,
String
parentColumnNames
,
String
childColumnNames
,
AcceptRejectRule
acceptRejectRule
,
Rule
deleteRule
,
Rule
updateRule
)
|
public |
ForeignKeyConstraint |
(
String
constraintName
,
String
parentTableName
,
String
parentTableNamespace
,
String
parentColumnNames
,
String
childColumnNames
,
AcceptRejectRule
acceptRejectRule
,
Rule
deleteRule
,
Rule
updateRule
)
|
|
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 initializes a ForeignKeyConstraint, sets various of its properties, and adds it to a DataTable object' s ConstraintCollection.
[ VB ]' the next line goes into the Declarations section of the module:
' suppliersProducts is a class derived from DataSet.
Private myDataSet As SuppliersProducts
Private Sub SetConstraint ( )
' 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 ( "SupplierFkConst", 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
Constraint Constraints ConstraintCollection DataRelation UniqueConstraint