DataTable.Constraints DataTable.Constraints DataTable.Constraints DataTable.Constraints Property

定義

取得這個資料表所維護的條件約束 (Constraint) 集合。Gets the collection of constraints maintained by this table.

public:
 property System::Data::ConstraintCollection ^ Constraints { System::Data::ConstraintCollection ^ get(); };
[System.Data.DataSysDescription("DataTableConstraintsDescr")]
public System.Data.ConstraintCollection Constraints { get; }
member this.Constraints : System.Data.ConstraintCollection
Public ReadOnly Property Constraints As ConstraintCollection

屬性值

ConstraintCollection,其中包含資料表的 Constraint 物件集合。A ConstraintCollection that contains the collection of Constraint objects for the table. 如果沒有任何 Constraint 物件存在,則會傳回空的集合。An empty collection is returned if no Constraint objects exist.

範例

下列範例會將加入ForeignKeyConstraint條件約束的集合。The following example adds a ForeignKeyConstraint to the collection of constraints.

private void CreateConstraint(DataSet dataSet, 
    string table1, string table2, string column1, string column2)
{
    ForeignKeyConstraint idKeyRestraint = new 
        ForeignKeyConstraint(dataSet.Tables[table1].Columns[column1],
        dataSet.Tables[table2].Columns[column2]);

    // Set null values when a value is deleted.
    idKeyRestraint.DeleteRule = Rule.SetNull;
    idKeyRestraint.UpdateRule = Rule.Cascade;

    // Set AcceptRejectRule to cascade changes.
    idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade;

    dataSet.Tables[table1].Constraints.Add(idKeyRestraint);
    dataSet.EnforceConstraints = true;
}
Private Sub CreateConstraint(dataSet As DataSet, _
    table1 As String, table2 As String, _
    column1 As String, column2 As String)

    Dim idKeyRestraint As ForeignKeyConstraint = _
        New ForeignKeyConstraint _
        (dataSet.Tables(table1).Columns(column1), _
        dataSet.Tables(table2).Columns(column2))

    ' Set null values when a value is deleted.
    idKeyRestraint.DeleteRule = Rule.SetNull
    idKeyRestraint.UpdateRule = Rule.Cascade

    ' Set AcceptRejectRule to cascade changes.
    idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade
    
    dataSet.Tables(table1).Constraints.Add(idKeyRestraint)
    dataSet.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.

嘗試將主鍵中的值設定為非唯一值時,會變成作用中。UniqueConstraintA UniqueConstraint becomes active when attempting to set a value in a primary key to a non-unique value.

適用於

另請參閱