ConstraintCollection.Add Method

Definition

Adds a Constraint object to the collection.

Overloads

Add(Constraint)

Adds the specified Constraint object to the collection.

Add(String, DataColumn, Boolean)

Constructs a new UniqueConstraint with the specified name, DataColumn, and value that indicates whether the column is a primary key, and adds it to the collection.

Add(String, DataColumn, DataColumn)

Constructs a new ForeignKeyConstraint with the specified name, parent column, and child column, and adds the constraint to the collection.

Add(String, DataColumn[], Boolean)

Constructs a new UniqueConstraint with the specified name, array of DataColumn objects, and value that indicates whether the column is a primary key, and adds it to the collection.

Add(String, DataColumn[], DataColumn[])

Constructs a new ForeignKeyConstraint, with the specified arrays of parent columns and child columns, and adds the constraint to the collection.

Add(Constraint)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs

Adds the specified Constraint object to the collection.

C#
public void Add(System.Data.Constraint constraint);

Parameters

constraint
Constraint

The Constraint to add.

Exceptions

The constraint argument is null.

The constraint already belongs to this collection, or belongs to another collection.

The collection already has a constraint with the same name. (The comparison is not case-sensitive.)

Examples

The following example adds a UniqueConstraint to the ConstraintCollection of a DataTable.

C#
private void AddConstraint(DataTable table)
{
    UniqueConstraint uniqueConstraint;
    // Assuming a column named "UniqueColumn" exists, and
    // its Unique property is true.
    uniqueConstraint = new UniqueConstraint(
        table.Columns["UniqueColumn"]);
    table.Constraints.Add(uniqueConstraint);
}

Remarks

If the collection is successfully changed by adding or removing constraints, the CollectionChanged event occurs.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, DataColumn, Boolean)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs

Constructs a new UniqueConstraint with the specified name, DataColumn, and value that indicates whether the column is a primary key, and adds it to the collection.

C#
public System.Data.Constraint Add(string? name, System.Data.DataColumn column, bool primaryKey);
C#
public System.Data.Constraint Add(string name, System.Data.DataColumn column, bool primaryKey);
C#
public virtual System.Data.Constraint Add(string name, System.Data.DataColumn column, bool primaryKey);

Parameters

name
String

The name of the UniqueConstraint.

column
DataColumn

The DataColumn to which the constraint applies.

primaryKey
Boolean

Specifies whether the column should be the primary key. If true, the column will be a primary key column.

Returns

A new UniqueConstraint.

Exceptions

The constraint already belongs to this collection.

-Or-

The constraint belongs to another collection.

The collection already has a constraint with the specified name. (The comparison is not case-sensitive.)

Examples

The following example uses the Add method to create and add a new UniqueConstraint to a ConstraintCollection.

C#
private void AddUniqueConstraint(DataTable table){
   table.Constraints.Add("idConstraint", table.Columns["id"], true);
}

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, DataColumn, DataColumn)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs

Constructs a new ForeignKeyConstraint with the specified name, parent column, and child column, and adds the constraint to the collection.

C#
public System.Data.Constraint Add(string? name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
C#
public System.Data.Constraint Add(string name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
C#
public virtual System.Data.Constraint Add(string name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);

Parameters

name
String

The name of the ForeignKeyConstraint.

primaryKeyColumn
DataColumn

The primary key, or parent, DataColumn.

foreignKeyColumn
DataColumn

The foreign key, or child, DataColumn.

Returns

A new ForeignKeyConstraint.

Examples

The following example adds a new ForeignKeyConstraint to the ConstraintCollection of a DataTable.

C#
private void AddForeignConstraint(DataSet dataSet)
{
    try
    {
        DataColumn parentColumn =
            dataSet.Tables["Suppliers"].Columns["SupplierID"];
        DataColumn childColumn =
            dataSet.Tables["Products"].Columns["SupplierID"];
        dataSet.Tables["Products"].Constraints.Add
            ("ProductsSuppliers", parentColumn, childColumn);
    }
    catch(Exception ex)
    {
        // In case the constraint already exists,
        // catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.",
            ex.GetType());
    }
}

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, DataColumn[], Boolean)

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs

Constructs a new UniqueConstraint with the specified name, array of DataColumn objects, and value that indicates whether the column is a primary key, and adds it to the collection.

C#
public System.Data.Constraint Add(string? name, System.Data.DataColumn[] columns, bool primaryKey);
C#
public System.Data.Constraint Add(string name, System.Data.DataColumn[] columns, bool primaryKey);
C#
public virtual System.Data.Constraint Add(string name, System.Data.DataColumn[] columns, bool primaryKey);

Parameters

name
String

The name of the UniqueConstraint.

columns
DataColumn[]

An array of DataColumn objects to which the constraint applies.

primaryKey
Boolean

Specifies whether the column should be the primary key. If true, the column will be a primary key column.

Returns

A new UniqueConstraint.

Exceptions

The constraint already belongs to this collection.

-Or-

The constraint belongs to another collection.

The collection already has a constraint with the specified name. (The comparison is not case-sensitive.)

Examples

The following example creates an array of DataColumn objects that are used to create a new UniqueConstraint in a specific DataTable.

C#
private void AddUniqueConstraint(DataTable table)
{
    DataColumn[] columns = new DataColumn[1];
    columns[0] = table.Columns["ID"];
    columns[1] = table.Columns["Name"];
    table.Constraints.Add("idNameConstraint", columns, true);
}

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, DataColumn[], DataColumn[])

Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs
Source:
ConstraintCollection.cs

Constructs a new ForeignKeyConstraint, with the specified arrays of parent columns and child columns, and adds the constraint to the collection.

C#
public System.Data.Constraint Add(string? name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
C#
public System.Data.Constraint Add(string name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
C#
public virtual System.Data.Constraint Add(string name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);

Parameters

name
String

The name of the ForeignKeyConstraint.

primaryKeyColumns
DataColumn[]

An array of DataColumn objects that are the primary key, or parent, columns.

foreignKeyColumns
DataColumn[]

An array of DataColumn objects that are the foreign key, or child, columns.

Returns

A new ForeignKeyConstraint.

Examples

The following example creates two arrays of DataColumn objects, and then creates two ForeignKeyConstraint relationships between two tables in a dataset.

C#
private void AddForeignConstraint(
    DataSet dataSet, DataTable table)
{
    try
    {
        DataColumn[] parentColumns = new DataColumn[2];
        DataColumn[] childColumns = new DataColumn[2];
        // Get the tables from the DataSet.
        DataTable customersTable = dataSet.Tables["Customers"];
        DataTable ordersTable = dataSet.Tables["Orders"];

        // Set Columns.
        parentColumns[0]=customersTable.Columns["id"];
        parentColumns[1]=customersTable.Columns["Name"];
        childColumns[0] = ordersTable.Columns["CustomerID"];
        childColumns[1] = ordersTable.Columns["CustomerName"];

        // Create ForeignKeyConstraint
        table.Constraints.Add("CustOrdersConstraint",
            parentColumns, childColumns);
    }
    catch(Exception ex)
    {
        // In case the constraint already exists,
        // catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.",
            ex.GetType());
    }
}

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1