ForeignKeyConstraint ForeignKeyConstraint ForeignKeyConstraint ForeignKeyConstraint Class

定义

表示删除或更新某个值或行时,对主键/外键关系中一组列强制进行的操作限制。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
[System.Serializable]
public class ForeignKeyConstraint : System.Data.Constraint
type ForeignKeyConstraint = class
    inherit Constraint
Public Class ForeignKeyConstraint
Inherits Constraint
继承
ForeignKeyConstraintForeignKeyConstraintForeignKeyConstraintForeignKeyConstraint
属性

示例

下面的示例创建一个ForeignKeyConstraint, 设置其一些属性, 并将其添加DataTable到对象的ConstraintCollectionThe 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

注解

ForeignKeyConstraint限制在删除或更新列中的值时执行的操作。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).

  • 子列中的值可以设置为 null 值。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对象包含在ConstraintCollection DataTable的中, 可通过Constraints属性访问。ForeignKeyConstraint objects are contained in the ConstraintCollection of a DataTable, which is accessed through the Constraints property.

除非将EnforceConstraints属性设置为true, 否则不会强制实施约束。Constraints are not enforced unless the EnforceConstraints property is set to true.

每次调用AcceptChanges AcceptRejectRule 对象的方法时,DataTable都会强制执行。The AcceptRejectRule is enforced whenever a DataTable object's AcceptChanges method is invoked.

构造函数

ForeignKeyConstraint(DataColumn, DataColumn) ForeignKeyConstraint(DataColumn, DataColumn) ForeignKeyConstraint(DataColumn, DataColumn) ForeignKeyConstraint(DataColumn, DataColumn)

使用指定的父级和子级 ForeignKeyConstraint 对象初始化 DataColumn 类的新实例。Initializes a new instance of the ForeignKeyConstraint class with the specified parent and child DataColumn objects.

ForeignKeyConstraint(DataColumn[], DataColumn[]) ForeignKeyConstraint(DataColumn[], DataColumn[]) ForeignKeyConstraint(DataColumn[], DataColumn[]) ForeignKeyConstraint(DataColumn[], DataColumn[])

使用指定的父级和子级 ForeignKeyConstraint 对象的数组,初始化 DataColumn 类的新实例。Initializes a new instance of the ForeignKeyConstraint class with the specified arrays of parent and child DataColumn objects.

ForeignKeyConstraint(String, DataColumn, DataColumn) ForeignKeyConstraint(String, DataColumn, DataColumn) ForeignKeyConstraint(String, DataColumn, DataColumn) ForeignKeyConstraint(String, DataColumn, DataColumn)

使用指定的名称、父级和子级 ForeignKeyConstraint 对象初始化 DataColumn 类的新实例。Initializes a new instance of the ForeignKeyConstraint class with the specified name, parent and child DataColumn objects.

ForeignKeyConstraint(String, DataColumn[], DataColumn[]) ForeignKeyConstraint(String, DataColumn[], DataColumn[]) ForeignKeyConstraint(String, DataColumn[], DataColumn[]) ForeignKeyConstraint(String, DataColumn[], DataColumn[])

使用指定的名称、父级和子级 ForeignKeyConstraint 对象的数组初始化 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) ForeignKeyConstraint(String, String, String, String[], String[], AcceptRejectRule, Rule, Rule) ForeignKeyConstraint(String, String, String, String[], String[], AcceptRejectRule, Rule, Rule) ForeignKeyConstraint(String, String, String, String[], String[], AcceptRejectRule, Rule, Rule)

此构造函数在 Visual Studio 环境中提供设计时支持。This constructor is provided for design time support in the Visual Studio environment. 因此,使用此构造函数创建的 ForeignKeyConstraint 对象必须通过 AddRange(Constraint[]) 添加到集合中。ForeignKeyConstraint objects created by using this constructor must then be added to the collection via AddRange(Constraint[]). 当调用该方法时,必须存在具有指定名称的表和列,或者如果在调用该构造函数之前已经调用了 BeginInit(),则在调用 EndInit() 时,必须存在具有指定名称的表和列。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) ForeignKeyConstraint(String, String, String[], String[], AcceptRejectRule, Rule, Rule) ForeignKeyConstraint(String, String, String[], String[], AcceptRejectRule, Rule, Rule) ForeignKeyConstraint(String, String, String[], String[], AcceptRejectRule, Rule, Rule)

此构造函数在 Visual Studio 环境中提供设计时支持。This constructor is provided for design time support in the Visual Studio environment. 因此,使用此构造函数创建的 ForeignKeyConstraint 对象必须通过 AddRange(Constraint[]) 添加到集合中。ForeignKeyConstraint objects created by using this constructor must then be added to the collection via AddRange(Constraint[]). 当调用该方法时,必须存在具有指定名称的表和列,或者如果在调用该构造函数之前已经调用了 BeginInit(),则在调用 EndInit() 时,必须存在具有指定名称的表和列。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.

属性

AcceptRejectRule AcceptRejectRule AcceptRejectRule AcceptRejectRule

指示调用 AcceptChanges() 时,通过该约束应发生的操作。Indicates the action that should take place across this constraint when AcceptChanges() is invoked.

Columns Columns Columns Columns

获取该约束的子列。Gets the child columns of this constraint.

ConstraintName ConstraintName ConstraintName ConstraintName

ConstraintCollection 中的约束的名称。The name of a constraint in the ConstraintCollection.

(Inherited from Constraint)
DeleteRule DeleteRule DeleteRule DeleteRule

获取或设置当删除某一行时通过该约束发生的操作。Gets or sets the action that occurs across this constraint when a row is deleted.

ExtendedProperties ExtendedProperties ExtendedProperties ExtendedProperties

获取用户定义的约束属性的集合。Gets the collection of user-defined constraint properties.

(Inherited from Constraint)
RelatedColumns RelatedColumns RelatedColumns RelatedColumns

该约束的父列。The parent columns of this constraint.

RelatedTable RelatedTable RelatedTable RelatedTable

获取该约束的父表。Gets the parent table of this constraint.

Table Table Table Table

获取该约束的子表。Gets the child table of this constraint.

UpdateRule UpdateRule UpdateRule UpdateRule

获取或设置当更新某一行时通过该约束发生的操作。Gets or sets the action that occurs across this constraint on when a row is updated.

_DataSet _DataSet _DataSet _DataSet

获取此约束所属的 DataSetGets the DataSet to which this constraint belongs.

(Inherited from Constraint)

方法

CheckStateForProperty() CheckStateForProperty() CheckStateForProperty() CheckStateForProperty()

获取此约束所属的 DataSetGets the DataSet to which this constraint belongs.

(Inherited from Constraint)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

获取一个值,该值指示当前的 ForeignKeyConstraint 是否与指定对象相同。Gets a value indicating whether the current ForeignKeyConstraint is identical to the specified object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

获取 ForeignKeyConstraint 对象的此实例的哈希代码。Gets the hash code of this instance of the ForeignKeyConstraint object.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
SetDataSet(DataSet) SetDataSet(DataSet) SetDataSet(DataSet) SetDataSet(DataSet)

设置约束的 DataSetSets the constraint's DataSet.

(Inherited from Constraint)
ToString() ToString() ToString() ToString()

获取 ConstraintName(如果存在)作为字符串。Gets the ConstraintName, if there is one, as a string.

(Inherited from Constraint)

适用于

线程安全性

对于多线程读取操作, 此类型是安全的。This type is safe for multithreaded read operations. 必须同步任何写入操作。You must synchronize any write operations.

另请参阅