This documentation is archived and is not being maintained.

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

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.



The name of the ForeignKeyConstraint.


The primary key, or parent, DataColumn.


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)
        DataColumn parentColumn = 
        DataColumn childColumn = 
            ("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.", 

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