UniqueConstraint
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(DataColumncolumn
); public UniqueConstraint(DataColumn[ ]columns
); public UniqueConstraint(DataColumn[ ]columns
, boolisPrimaryKey
); public UniqueConstraint(DataColumncolumn
, boolisPrimaryKey
); public UniqueConstraint(stringname
, DataColumncolumn
); public UniqueConstraint(stringname
, DataColumn[ ]columns
); public UniqueConstraint(stringname
, DataColumn[ ]columns
, boolisPrimaryKey
); public UniqueConstraint(stringname
, DataColumncolumn
, boolisPrimaryKey
); public UniqueConstraint(stringname
, string[ ]columnNames
, boolisPrimaryKey
); // Public Instance Properties public virtual DataColumn[ ] Columns{get; } public bool IsPrimaryKey{get; } public override DataTable Table{get; } // overrides Constraint // Public Instance Methods public override bool Equals(objectkey2
); // overrides object public override int GetHashCode( ); // overrides object }