This documentation is archived and is not being maintained.

ConstraintCollection.Add Method (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.

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

public Constraint Add (
	string name,
	DataColumn column,
	bool primaryKey
)
public Constraint Add (
	String name, 
	DataColumn column, 
	boolean primaryKey
)
public function Add (
	name : String, 
	column : DataColumn, 
	primaryKey : boolean
) : Constraint

Parameters

name

The name of the UniqueConstraint.

column

The DataColumn to which the constraint applies.

primaryKey

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

Return Value

A new UniqueConstraint.

Exception typeCondition

ArgumentException

The constraint already belongs to this collection.

-Or-

The constraint belongs to another collection.

DuplicateNameException

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

The CollectionChanged event occurs if the constraint is added successfully.

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


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: