ConstraintCollection.AddRange(Constraint[]) 方法

定义

将指定的 ConstraintCollection 数组的元素复制到集合末尾。

public:
 void AddRange(cli::array <System::Data::Constraint ^> ^ constraints);
public void AddRange (System.Data.Constraint[]? constraints);
public void AddRange (System.Data.Constraint[] constraints);
member this.AddRange : System.Data.Constraint[] -> unit
Public Sub AddRange (constraints As Constraint())

参数

constraints
Constraint[]

要添加到集合中的 ConstraintCollection 对象的数组。

示例

以下示例创建主键和外键约束,并将其添加到 。ConstraintCollection

public static void ConstraintAddRange(DataSet dataSet)
{
    try
    {
        // Reference the tables from the DataSet.
        DataTable customersTable = dataSet.Tables["Customers"];
        DataTable ordersTable = dataSet.Tables["Orders"];

        // Create unique and foreign key constraints.
        UniqueConstraint uniqueConstraint = new
            UniqueConstraint(customersTable.Columns["CustomerID"]);
        ForeignKeyConstraint fkConstraint = new
            ForeignKeyConstraint("CustOrdersConstraint",
            customersTable.Columns["CustomerID"],
            ordersTable.Columns["CustomerID"]);

        // Add the constraints.
        customersTable.Constraints.AddRange(new Constraint[]
            {uniqueConstraint, fkConstraint});
    }
    catch(Exception ex)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.",
            ex.GetType());
    }
}
Public Shared Sub ConstraintAddRange(dataSet As DataSet)
    Try
        ' Reference the tables from the DataSet.
        Dim customersTable As DataTable = dataSet.Tables("Customers")
        Dim ordersTable As DataTable = dataSet.Tables("Orders")

        ' Create unique and foreign key constraints.
        Dim uniqueConstraint As New UniqueConstraint( _
            customersTable.Columns("CustomerID"))
        Dim fkConstraint As New ForeignKeyConstraint("CustOrdersConstraint", _
            customersTable.Columns("CustomerID"), _
            ordersTable.Columns("CustomerID"))

        ' Add the constraints.
        customersTable.Constraints.AddRange(New Constraint() _
            {uniqueConstraint, fkConstraint})

    Catch ex As Exception
        ' Process exception and return.
        Console.WriteLine($"Exception of type {ex.GetType()} occurred.")
    End Try
End Sub

注解

如果 BeginInit 已调用 , AddRange 则在调用 之前 EndInit 不会向集合添加任何对象。 调用 时 EndInit ,集合将使用最近调用 AddRange中指定的项填充。 如果在AddRange序列EndInitBeginInit / 中多次调用 ,则仅添加在最近调用 中指定的AddRange项。

适用于