共用方式為


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 中指定的專案。

適用於