This documentation is archived and is not being maintained.

ConstraintCollection.AddRange Method

Copies the elements of the specified ConstraintCollection array to the end of the collection.

[Visual Basic]
Public Sub AddRange( _
   ByVal constraints() As Constraint _
)
[C#]
public void AddRange(
 Constraint[] constraints
);
[C++]
public: void AddRange(
 Constraint* constraints[]
);
[JScript]
public function AddRange(
   constraints : Constraint[]
);

Parameters

constraints
An array of ConstraintCollection objects to add to the collection.

Remarks

If BeginInit has been called, AddRange does not add any objects to the collection until EndInit is called. At the time that EndInit is called, the collection will be populated with the items specified in the most recent call to AddRange. If AddRange is called multiple times within a BeginInit/ EndInit sequence, only those items specified in the most recent call to AddRange are added.

Example

[Visual Basic, C#, C++] The following example creates primary and foreign key constraints, and adds them to the ConstraintCollection.

[Visual Basic] 
Public Shared Sub ConstraintAddRange(myDataSet As DataSet)
    Try
        ' Reference the tables from the DataSet.
        Dim tCustomers As DataTable = myDataSet.Tables("Customers")
        Dim tOrders As DataTable = myDataSet.Tables("Orders")

        ' Create unique and foreign key constraints.
        Dim uc As UniqueConstraint = New UniqueConstraint(tCustomers.Columns("CustomerID"))
        Dim fk As ForeignKeyConstraint = New ForeignKeyConstraint("CustOrdersConstraint", _
                                                       tCustomers.Columns("CustomerID"), _
                                                       tOrders.Columns("CustomerID"))

        ' Add the constraints.
        tCustomers.Constraints.AddRange(New Constraint() {uc, fk})
    Catch e As Exception
        ' Process exception and return.
                Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
                log.Source = "My Application"
                log.WriteEntry(e.ToString())
                Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
    End Try
End Sub

[C#] 
public static void ConstraintAddRange(DataSet myDataSet)
{
    try
    {
        // Reference the tables from the DataSet.
        DataTable tCustomers = myDataSet.Tables["Customers"];
        DataTable tOrders = myDataSet.Tables["Orders"];

        // Create unique and foreign key constraints.
        UniqueConstraint uc = new UniqueConstraint(tCustomers.Columns["CustomerID"]);
        ForeignKeyConstraint fk = new ForeignKeyConstraint("CustOrdersConstraint",
                                                                   tCustomers.Columns["CustomerID"],
                                                                   tOrders.Columns["CustomerID"]);

        // Add the constraints.
        tCustomers.Constraints.AddRange(new Constraint[] {uc, fk});
    }
    catch(Exception e)
    {
        // Process exception and return.
                System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
                log.Source = "My Application";
                log.WriteEntry(e.ToString());
                Console.WriteLine("Exception of type {0} occurred.", e.GetType());
    }
}

[C++] 
public:
    static void ConstraintAddRange(DataSet* myDataSet)
    {
        try
        {
            // Reference the tables from the DataSet.
            DataTable* tCustomers = myDataSet->Tables->Item[S"Customers"];
            DataTable* tOrders = myDataSet->Tables->Item[S"Orders"];

            // Create unique and foreign key constraints.
            UniqueConstraint* uc = new UniqueConstraint(tCustomers->Columns->Item[S"CustomerID"]);
            ForeignKeyConstraint* fk = new ForeignKeyConstraint(S"CustOrdersConstraint",
                tCustomers->Columns->Item[S"CustomerID"],
                tOrders->Columns->Item[S"CustomerID"]);

            // Add the constraints.
            Constraint* constraintArray[] = {uc, fk};
            tCustomers->Constraints->AddRange(constraintArray);
        }
        catch (Exception* e)
        {
            // Process exception and return.
            System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
            log->Source = S"My Application";
            log->WriteEntry(e->ToString());
            Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
        }
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

ConstraintCollection Class | ConstraintCollection Members | System.Data Namespace

Show: