ConstraintCollection.Add 方法

定义

Constraint 对象添加到集合中。

重载

Add(Constraint)

将指定的 Constraint 对象添加到集合。

Add(String, DataColumn, Boolean)

使用指定名称、DataColumn 和指示列是否为主键的值构造一个新 UniqueConstraint,并将其添加到集合中。

Add(String, DataColumn, DataColumn)

使用指定名称、父列和子列来构造新 ForeignKeyConstraint,并将此约束添加到集合。

Add(String, DataColumn[], Boolean)

使用指定名称、DataColumn 对象的数组和指示列是否为主键的值构造一个新 UniqueConstraint,并将其添加到集合中。

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

使用指定的父列和子列数组,构造新的 ForeignKeyConstraint,并将该约束添加到集合。

Add(Constraint)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
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。

约束已属于此集合,或属于其他集合。

集合已具有相同名称的约束。 (该比较不区分大小写。)

示例

以下示例将 添加到 UniqueConstraintDataTableConstraintCollection

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)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs

使用指定名称、DataColumn 和指示列是否为主键的值构造一个新 UniqueConstraint,并将其添加到集合中。

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)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
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)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs

使用指定名称、DataColumn 对象的数组和指示列是否为主键的值构造一个新 UniqueConstraint,并将其添加到集合中。

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[])

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
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如果成功添加约束,则会发生 该事件。

适用于