ForeignKeyConstraint Class System.Data Namespace
Sets or retrieves the action that occurs across this constraint when a row is deleted.
[ VB ]
Overridable Public Property DeleteRule As Rule
[ C# ]
public Rule DeleteRule {virtual get; virtual set;}
[ C++ ]
public: __property virtual Rule get_DeleteRule ( );
public: __property virtual void set_DeleteRule ( Rule );
[ JScript ]
public function get DeleteRule ( ) : Rule;
public function set DeleteRule ( Rule );
One of the Rule values. The default is Cascade. Possible values include: None, Cascade, SetNull, SetDefault, and Default.
When a row is deleted from a parent table, the DeleteRule determines what will happen in the columns of the child table ( or tables ). If the rule is set to Cascade, child rows will be deleted.
If set to SetNull, a DBnull will be placed in the appropriate columns of the affected rows. Depending on your DBMS, a null value may or may not be permitted in a column. For example, SQL Server™ allows multiple null values to be found in a primary key column, even if they are not unique. In a DataTable, however, if a DataColumn object's Unique property is set to true, multiple null values are not allowed in primary key columns.
If set to Default, the default value for the column is assigned.
The following example initializes a ForeignKeyConstraint, sets various of its properties, and adds it to a DataTable object's ConstraintsCollection.
[ 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 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