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 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

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: