The UniqueConstraint is a rule that prevents duplicate values in a column. There are two ways to create a UniqueConstraint: by setting the DataColumn.Unique property to true for one or more columns, or by using the IDataAdapter.FillSchema( ) method when retrieving a row set that includes a primary key column. You could also create a UniqueConstraint object using the new keyword, however, you need to add it to the DataTable.Constraints collection before it takes effect; simply specifying the column and table information isn’t enough. As with all constraints, the UniqueConstraint is enforced only when the DataSet.EnforceConstraints is true (the default).

public class UniqueConstraint : Constraint {

// Public Constructors

   public UniqueConstraint(DataColumn column);  

   public UniqueConstraint(DataColumn[ ] columns);  

   public UniqueConstraint(DataColumn[ ] columns, bool isPrimaryKey);

   public UniqueConstraint(DataColumn column, bool isPrimaryKey);

   public UniqueConstraint(string name, DataColumn column);  

   public UniqueConstraint(string name, DataColumn[ ] columns);

   public UniqueConstraint(string name, DataColumn[ ] columns, bool isPrimaryKey);

   public UniqueConstraint(string name, DataColumn column, bool isPrimaryKey);

   public UniqueConstraint(string name, string[ ] columnNames, bool isPrimaryKey);

// Public Instance Properties

   public virtual DataColumn[ ] Columns{get; } 

   public bool IsPrimaryKey{get; } 

   public override DataTable Table{get; }              // overrides Constraint ...

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.