Breyta

Deila með


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.

public:
 void Add(System::Data::Constraint ^ constraint);
public void Add (System.Data.Constraint constraint);
member this.Add : System.Data.Constraint -> unit
Public Sub Add (constraint As 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.

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);
}
Private Sub AddConstraint(table As DataTable)
    Dim uniqueConstraint As UniqueConstraint
    ' Assuming a column named "UniqueColumn" exists, and 
    ' its Unique property is true.
    uniqueConstraint = _
       New UniqueConstraint(table.Columns("UniqueColumn"))
    table.Constraints.Add(uniqueConstraint)
End Sub

Remarks

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

See also

Applies to

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.

public:
 System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ column, bool primaryKey);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ column, bool primaryKey);
public System.Data.Constraint Add (string? name, System.Data.DataColumn column, bool primaryKey);
public System.Data.Constraint Add (string name, System.Data.DataColumn column, bool primaryKey);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn column, bool primaryKey);
member this.Add : string * System.Data.DataColumn * bool -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn * bool -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn * bool -> System.Data.Constraint
Public Function Add (name As String, column As DataColumn, primaryKey As Boolean) As Constraint
Public Overridable Function Add (name As String, column As DataColumn, primaryKey As Boolean) As Constraint

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.

private void AddUniqueConstraint(DataTable table){
   table.Constraints.Add("idConstraint", table.Columns["id"], true);
}
Private Sub AddUniqueConstraint(table As DataTable)
    table.Constraints.Add("idConstraint", table.Columns("id"), True)
End Sub

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

See also

Applies to

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.

public:
 System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ primaryKeyColumn, System::Data::DataColumn ^ foreignKeyColumn);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, System::Data::DataColumn ^ primaryKeyColumn, System::Data::DataColumn ^ foreignKeyColumn);
public System.Data.Constraint Add (string? name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
public System.Data.Constraint Add (string name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn primaryKeyColumn, System.Data.DataColumn foreignKeyColumn);
member this.Add : string * System.Data.DataColumn * System.Data.DataColumn -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn * System.Data.DataColumn -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn * System.Data.DataColumn -> System.Data.Constraint
Public Function Add (name As String, primaryKeyColumn As DataColumn, foreignKeyColumn As DataColumn) As Constraint
Public Overridable Function Add (name As String, primaryKeyColumn As DataColumn, foreignKeyColumn As DataColumn) As Constraint

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.

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());
    }
}
Private Sub AddForeignConstraint(dataSet As DataSet)
    Try
        Dim parentColumn As DataColumn = _
            dataSet.Tables("Suppliers").Columns("SupplierID")
        Dim childColumn As DataColumn = _
            dataSet.Tables("Products").Columns("SupplierID")
        dataSet.Tables("Products").Constraints.Add _
            ("ProductsSuppliers", parentColumn, childColumn)
        
    Catch ex As Exception
        ' In case the constraint already exists, 
        ' catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.", _
            ex.GetType().ToString())
    End Try
End Sub

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

Applies to

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.

public:
 System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ columns, bool primaryKey);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ columns, bool primaryKey);
public System.Data.Constraint Add (string? name, System.Data.DataColumn[] columns, bool primaryKey);
public System.Data.Constraint Add (string name, System.Data.DataColumn[] columns, bool primaryKey);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn[] columns, bool primaryKey);
member this.Add : string * System.Data.DataColumn[] * bool -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn[] * bool -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn[] * bool -> System.Data.Constraint
Public Function Add (name As String, columns As DataColumn(), primaryKey As Boolean) As Constraint
Public Overridable Function Add (name As String, columns As DataColumn(), primaryKey As Boolean) As Constraint

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.

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);
}
Private Sub AddUniqueConstraint(table As DataTable)
    Dim columns(1) As DataColumn
    columns(0) = table.Columns("ID")
    columns(1) = table.Columns("Name")
    table.Constraints.Add("idNameConstraint", columns, True)
End Sub

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

See also

Applies to

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.

public:
 System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ primaryKeyColumns, cli::array <System::Data::DataColumn ^> ^ foreignKeyColumns);
public:
 virtual System::Data::Constraint ^ Add(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ primaryKeyColumns, cli::array <System::Data::DataColumn ^> ^ foreignKeyColumns);
public System.Data.Constraint Add (string? name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
public System.Data.Constraint Add (string name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
public virtual System.Data.Constraint Add (string name, System.Data.DataColumn[] primaryKeyColumns, System.Data.DataColumn[] foreignKeyColumns);
member this.Add : string * System.Data.DataColumn[] * System.Data.DataColumn[] -> System.Data.Constraint
abstract member Add : string * System.Data.DataColumn[] * System.Data.DataColumn[] -> System.Data.Constraint
override this.Add : string * System.Data.DataColumn[] * System.Data.DataColumn[] -> System.Data.Constraint
Public Function Add (name As String, primaryKeyColumns As DataColumn(), foreignKeyColumns As DataColumn()) As Constraint
Public Overridable Function Add (name As String, primaryKeyColumns As DataColumn(), foreignKeyColumns As DataColumn()) As Constraint

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.

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());
    }
}
Private Sub AddForeignConstraint( _
ByVal dataSet As DataSet, ByVal table As DataTable)
    Try
        Dim parentColumns(1) As DataColumn
        Dim childColumns(1) As DataColumn
        ' Get the tables from the DataSet.
        Dim customersTable As DataTable = _
            dataSet.Tables("Customers")
        Dim ordersTable As DataTable = _
            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 ex As Exception
        ' In case the constraint already exists, 
        ' catch the collision here and respond.
        Console.WriteLine("Exception of type {0} occurred.", _
            ex.GetType().ToString())
    End Try
End Sub

Remarks

The CollectionChanged event occurs if the constraint is added successfully.

Applies to