共用方式為


ConstraintCollection.Add 方法

定義

Constraint 物件加入至集合。

多載

Add(Constraint)

將指定的 Constraint 物件加入至集合。

Add(String, DataColumn, Boolean)

使用指定的名稱、UniqueConstraint 和表示資料行是否為主索引鍵的值,來建構新的 DataColumn,並將它加入至集合中。

Add(String, DataColumn, DataColumn)

使用指定的名稱、父資料行和子資料行建構新的 ForeignKeyConstraint,並將條件約束加入至集合。

Add(String, DataColumn[], Boolean)

使用指定的名稱、UniqueConstraint 物件的陣列,以及表示資料行是否為主索引鍵的值,來建構新的 DataColumn,並將它加入至集合中。

Add(String, DataColumn[], DataColumn[])

使用父資料行和子資料行的指定陣列,建構新的 ForeignKeyConstraint,並將條件約束加入至集合。

Add(Constraint)

來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs

將指定的 Constraint 物件加入至集合。

public:
 void Add(System::Data::Constraint ^ constraint);
public void Add (System.Data.Constraint constraint);
member this.Add : System.Data.Constraint -> unit
Public Sub Add (constraint As Constraint)

參數

constraint
Constraint

要加入的 Constraint

例外狀況

constraint 引數為 null。

條件約束已經屬於此集合,或屬於另一個集合。

集合已經有相同名稱的條件約束。 (這項比較不會區分大小寫。)

範例

下列範例會將 新增 UniqueConstraintConstraintCollectionDataTable

private void AddConstraint(DataTable table)
{
    UniqueConstraint uniqueConstraint;
    // Assuming a column named "UniqueColumn" exists, and
    // its Unique property is true.
    uniqueConstraint = new UniqueConstraint(
        table.Columns["UniqueColumn"]);
    table.Constraints.Add(uniqueConstraint);
}
Private Sub AddConstraint(table As DataTable)
    Dim uniqueConstraint As UniqueConstraint
    ' Assuming a column named "UniqueColumn" exists, and 
    ' its Unique property is true.
    uniqueConstraint = _
       New UniqueConstraint(table.Columns("UniqueColumn"))
    table.Constraints.Add(uniqueConstraint)
End Sub

備註

如果藉由新增或移除條件約束成功變更集合,就會 CollectionChanged 發生此事件。

另請參閱

適用於

Add(String, DataColumn, Boolean)

來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs

使用指定的名稱、UniqueConstraint 和表示資料行是否為主索引鍵的值,來建構新的 DataColumn,並將它加入至集合中。

public:
 System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ column, bool primaryKey);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ column, bool primaryKey);
public System.Data.Constraint Add (string? name, System.Data.DataColumn column, bool primaryKey);
public System.Data.Constraint Add (string name, System.Data.DataColumn column, bool primaryKey);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn column, bool primaryKey);
member this.Add : string * System.Data.DataColumn * bool -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn * bool -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn * bool -> System.Data.Constraint
Public Function Add (name As String, column As DataColumn, primaryKey As Boolean) As Constraint
Public Overridable Function Add (name As String, column As DataColumn, primaryKey As Boolean) As Constraint

參數

name
String

UniqueConstraint 的名稱。

column
DataColumn

條件約束套用至的 DataColumn

primaryKey
Boolean

指定資料行是否應為主索引鍵。 如果是 true,表示資料行將會是主索引鍵資料行。

傳回

新的 UniqueConstraint

例外狀況

條件約束已屬於這個集合。

-或-

條件約束屬於另一個集合。

集合已經有指定名稱的條件約束 (這項比較不會區分大小寫。)

範例

下列範例會 Add 使用 方法來建立並 UniqueConstraint 新增至 ConstraintCollection

private void AddUniqueConstraint(DataTable table){
   table.Constraints.Add("idConstraint", table.Columns["id"], true);
}
Private Sub AddUniqueConstraint(table As DataTable)
    table.Constraints.Add("idConstraint", table.Columns("id"), True)
End Sub

備註

CollectionChanged如果成功新增條件約束,就會發生此事件。

另請參閱

適用於

Add(String, DataColumn, DataColumn)

來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs

使用指定的名稱、父資料行和子資料行建構新的 ForeignKeyConstraint,並將條件約束加入至集合。

public:
 System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ primaryKeyColumn, System::Data::DataColumn ^ foreignKeyColumn);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ primaryKeyColumn, System::Data::DataColumn ^ foreignKeyColumn);
public System.Data.Constraint Add (string? name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
public System.Data.Constraint Add (string name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
member this.Add : string * System.Data.DataColumn * System.Data.DataColumn -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn * System.Data.DataColumn -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn * System.Data.DataColumn -> System.Data.Constraint
Public Function Add (name As String, primaryKeyColumn As DataColumn, foreignKeyColumn As DataColumn) As Constraint
Public Overridable Function Add (name As String, primaryKeyColumn As DataColumn, foreignKeyColumn As DataColumn) As Constraint

參數

name
String

ForeignKeyConstraint 的名稱。

primaryKeyColumn
DataColumn

主索引鍵,或父 DataColumn

foreignKeyColumn
DataColumn

外部索引鍵,或子 DataColumn

傳回

新的 ForeignKeyConstraint

範例

下列範例會將新的 ForeignKeyConstraint 新增至 ConstraintCollectionDataTable

private void AddForeignConstraint(DataSet dataSet)
{
    try
    {
        DataColumn parentColumn =
            dataSet.Tables["Suppliers"].Columns["SupplierID"];
        DataColumn childColumn =
            dataSet.Tables["Products"].Columns["SupplierID"];
        dataSet.Tables["Products"].Constraints.Add
            ("ProductsSuppliers", parentColumn, childColumn);
    }
    catch(Exception ex)
    {
        // In case the constraint already exists,
        // catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.",
            ex.GetType());
    }
}
Private Sub AddForeignConstraint(dataSet As DataSet)
    Try
        Dim parentColumn As DataColumn = _
            dataSet.Tables("Suppliers").Columns("SupplierID")
        Dim childColumn As DataColumn = _
            dataSet.Tables("Products").Columns("SupplierID")
        dataSet.Tables("Products").Constraints.Add _
            ("ProductsSuppliers", parentColumn, childColumn)
        
    Catch ex As Exception
        ' In case the constraint already exists, 
        ' catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.", _
            ex.GetType().ToString())
    End Try
End Sub

備註

CollectionChanged如果成功新增條件約束,就會發生此事件。

適用於

Add(String, DataColumn[], Boolean)

來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs

使用指定的名稱、UniqueConstraint 物件的陣列,以及表示資料行是否為主索引鍵的值,來建構新的 DataColumn,並將它加入至集合中。

public:
 System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ columns, bool primaryKey);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ columns, bool primaryKey);
public System.Data.Constraint Add (string? name, System.Data.DataColumn[] columns, bool primaryKey);
public System.Data.Constraint Add (string name, System.Data.DataColumn[] columns, bool primaryKey);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn[] columns, bool primaryKey);
member this.Add : string * System.Data.DataColumn[] * bool -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn[] * bool -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn[] * bool -> System.Data.Constraint
Public Function Add (name As String, columns As DataColumn(), primaryKey As Boolean) As Constraint
Public Overridable Function Add (name As String, columns As DataColumn(), primaryKey As Boolean) As Constraint

參數

name
String

UniqueConstraint 的名稱。

columns
DataColumn[]

條件約束套用至的 DataColumn 物件的陣列。

primaryKey
Boolean

指定資料行是否應為主索引鍵。 如果是 true,表示資料行將會是主索引鍵資料行。

傳回

新的 UniqueConstraint

例外狀況

條件約束已屬於這個集合。

-或-

條件約束屬於另一個集合。

集合已經有指定名稱的條件約束 (這項比較不會區分大小寫。)

範例

下列範例會建立 物件陣列DataColumn,這些物件是用來在特定 中DataTable建立新的 UniqueConstraint

private void AddUniqueConstraint(DataTable table)
{
    DataColumn[] columns = new DataColumn[1];
    columns[0] = table.Columns["ID"];
    columns[1] = table.Columns["Name"];
    table.Constraints.Add("idNameConstraint", columns, true);
}
Private Sub AddUniqueConstraint(table As DataTable)
    Dim columns(1) As DataColumn
    columns(0) = table.Columns("ID")
    columns(1) = table.Columns("Name")
    table.Constraints.Add("idNameConstraint", columns, True)
End Sub

備註

CollectionChanged如果成功新增條件約束,就會發生此事件。

另請參閱

適用於

Add(String, DataColumn[], DataColumn[])

來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs
來源:
ConstraintCollection.cs

使用父資料行和子資料行的指定陣列,建構新的 ForeignKeyConstraint,並將條件約束加入至集合。

public:
 System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ primaryKeyColumns, cli::array <System::Data::DataColumn ^> ^ foreignKeyColumns);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ primaryKeyColumns, cli::array <System::Data::DataColumn ^> ^ foreignKeyColumns);
public System.Data.Constraint Add (string? name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
public System.Data.Constraint Add (string name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
member this.Add : string * System.Data.DataColumn[] * System.Data.DataColumn[] -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn[] * System.Data.DataColumn[] -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn[] * System.Data.DataColumn[] -> System.Data.Constraint
Public Function Add (name As String, primaryKeyColumns As DataColumn(), foreignKeyColumns As DataColumn()) As Constraint
Public Overridable Function Add (name As String, primaryKeyColumns As DataColumn(), foreignKeyColumns As DataColumn()) As Constraint

參數

name
String

ForeignKeyConstraint 的名稱。

primaryKeyColumns
DataColumn[]

DataColumn 物件 (為主索引鍵或父資料行) 的陣列。

foreignKeyColumns
DataColumn[]

DataColumn 物件 (為外部索引鍵或子資料行) 的陣列。

傳回

新的 ForeignKeyConstraint

範例

下列範例會建立兩個 DataColumn 對象的數位,然後在數據集中的兩個數據表之間建立兩 ForeignKeyConstraint 個關聯性。

private void AddForeignConstraint(
    DataSet dataSet, DataTable table)
{
    try
    {
        DataColumn[] parentColumns = new DataColumn[2];
        DataColumn[] childColumns = new DataColumn[2];
        // Get the tables from the DataSet.
        DataTable customersTable = dataSet.Tables["Customers"];
        DataTable ordersTable = dataSet.Tables["Orders"];

        // Set Columns.
        parentColumns[0]=customersTable.Columns["id"];
        parentColumns[1]=customersTable.Columns["Name"];
        childColumns[0] = ordersTable.Columns["CustomerID"];
        childColumns[1] = ordersTable.Columns["CustomerName"];

        // Create ForeignKeyConstraint
        table.Constraints.Add("CustOrdersConstraint",
            parentColumns, childColumns);
    }
    catch(Exception ex)
    {
        // In case the constraint already exists,
        // catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.",
            ex.GetType());
    }
}
Private Sub AddForeignConstraint( _
ByVal dataSet As DataSet, ByVal table As DataTable)
    Try
        Dim parentColumns(1) As DataColumn
        Dim childColumns(1) As DataColumn
        ' Get the tables from the DataSet.
        Dim customersTable As DataTable = _
            dataSet.Tables("Customers")
        Dim ordersTable As DataTable = _
            dataSet.Tables("Orders")

        ' Set Columns.
        parentColumns(0) = customersTable.Columns("id")
        parentColumns(1) = customersTable.Columns("Name")
        childColumns(0) = ordersTable.Columns("CustomerID")
        childColumns(1) = ordersTable.Columns("CustomerName")

        ' Create ForeignKeyConstraint
        table.Constraints.Add("CustOrdersConstraint", _
            parentColumns, childColumns)

    Catch ex As Exception
        ' In case the constraint already exists, 
        ' catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.", _
            ex.GetType().ToString())
    End Try
End Sub

備註

CollectionChanged如果成功新增條件約束,就會發生此事件。

適用於