Bagikan melalui


ConstraintCollection.AddRange(Constraint[]) Metode

Definisi

Menyalin elemen array yang ditentukan ConstraintCollection ke akhir koleksi.

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())

Parameter

constraints
Constraint[]

Array ConstraintCollection objek untuk ditambahkan ke koleksi.

Contoh

Contoh berikut membuat batasan kunci primer dan asing, dan menambahkannya ke 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

Keterangan

Jika BeginInit telah dipanggil, AddRange jangan tambahkan objek apa pun ke koleksi hingga EndInit dipanggil. Pada saat dipanggil EndInit , koleksi akan diisi dengan item yang ditentukan dalam panggilan terbaru ke AddRange. Jika AddRange dipanggil beberapa kali dalam BeginInit / EndInit urutan, hanya item yang ditentukan dalam panggilan terbaru yang AddRange ditambahkan.

Berlaku untuk