Rule 枚举


指示强制执行 ForeignKeyConstraint 时发生的操作。Indicates the action that occurs when a ForeignKeyConstraint is enforced.

public enum class Rule
public enum Rule
type Rule = 
Public Enum Rule


Cascade 1

删除或更新相关的行。Delete or update related rows. 这是默认设置。This is the default.

None 0

不对相关的行采取任何操作。No action taken on related rows.

SetDefault 3

将相关的行中的值设置为 DefaultValue 属性中包含的值。Set values in related rows to the value contained in the DefaultValue property.

SetNull 2

将相关的行中的值设置为 DBNullSet values in related rows to DBNull.


' 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.EnforceConstraints = True
End Sub


DataTable DeleteRule 值设置ForeignKeyConstraint为, 并且在对象的ConstraintCollection中找到的对象的属性。 UpdateRule RuleThe Rule values are set to the UpdateRule and the DeleteRule properties of a ForeignKeyConstraint object found in a DataTable object's ConstraintCollection.

Rule值确定在删除或更新列中的值时发生的操作。The Rule values determine the action that occurs when a value in a column is either deleted or updated. 对于这两种情况, 在设置规则时, 删除值就越关键并且需要注意。Of the two, deleting a value is the more critical and demanding of attention when setting a rule.

如果删除了值, Cascade则指定还将删除包含该值的所有行。In the case where a value is deleted, Cascade specifies that all rows containing that value are also deleted. SetNull指定所有子列中的值均设置为 null 值。SetNull specifies that values in all child columns are set to null values. SetDefault指定将所有子列都设置为列的默认值。SetDefault specifies that all child columns be set to the default value for the column. None指定不执行任何操作, 但生成异常。None specifies that no action will occur, but exceptions are generated.

如果值已更新, Cascade则指定所有子列同样使用新值进行更新。In the case where a value is updated, Cascade specifies that all child columns are likewise updated with the new value. SetNull指定所有子列都设置为 null 值。SetNull specifies that all child columns be set to null values. SetDefault指定将所有子列的值设置为默认值。SetDefault specifies that all child column values be set to the default value. None指定不执行任何操作, 但生成异常。None specifies that no action be taken, but exceptions are generated.

除非属性为DataSet ,true否则不会强制实施的约束。 EnforceConstraintsConstraints on a DataSet are not enforced unless the EnforceConstraints property is true.

调用方法时, 进一步确定发生AcceptRejectRule的操作。 AcceptChangesWhen the AcceptChanges method is called, the AcceptRejectRule further determines what action occurs.