Rule Rule Rule Rule Enum


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

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


Cascade Cascade Cascade Cascade 1

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

None None None None 0

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

SetDefault SetDefault SetDefault SetDefault 3

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

SetNull SetNull SetNull 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


Rule值设置为UpdateRuleDeleteRule的属性ForeignKeyConstraint中找到对象DataTable对象的ConstraintCollectionThe 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不会强制执行除非EnforceConstraints属性是trueConstraints on a DataSet are not enforced unless the EnforceConstraints property is true.

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