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 _
public void AddRange(
 Constraint[] constraints
public: void AddRange(
 Constraint* constraints[]
public function AddRange(
   constraints : Constraint[]


An array of ConstraintCollection objects to add to the collection.


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.


[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)
        ' 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"), _

        ' 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"
                Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
    End Try
End Sub

public static void ConstraintAddRange(DataSet myDataSet)
        // 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",

        // 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";
                Console.WriteLine("Exception of type {0} occurred.", e.GetType());

    static void ConstraintAddRange(DataSet* myDataSet)
            // 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",

            // Add the constraints.
            Constraint* constraintArray[] = {uc, fk};
        catch (Exception* e)
            // Process exception and return.
            System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
            log->Source = S"My Application";
            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.


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