Export (0) Print
Expand All

ConstraintCollection.Add Method (String, DataColumn, DataColumn)

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

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

public Constraint Add (
	string name,
	DataColumn primaryKeyColumn,
	DataColumn foreignKeyColumn
)
public Constraint Add (
	String name, 
	DataColumn primaryKeyColumn, 
	DataColumn foreignKeyColumn
)
public function Add (
	name : String, 
	primaryKeyColumn : DataColumn, 
	foreignKeyColumn : DataColumn
) : Constraint
Not applicable.

Parameters

name

The name of the ForeignKeyConstraint.

primaryKeyColumn

The primary key, or parent, DataColumn.

foreignKeyColumn

The foreign key, or child, DataColumn.

Return Value

A new ForeignKeyConstraint.

The CollectionChanged event occurs if the constraint is added successfully.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft