UniqueConstraint Class

Represents a restriction on a set of columns in which all values must be unique.

System.Object
  System.Data.Constraint
    System.Data.UniqueConstraint

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

public class UniqueConstraint : Constraint

The UniqueConstraint type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkUniqueConstraint(DataColumn)Initializes a new instance of the UniqueConstraint class with the specified DataColumn.
Public methodSupported by the XNA FrameworkUniqueConstraint(DataColumn[])Initializes a new instance of the UniqueConstraint class with the given array of DataColumn objects.
Public methodSupported by the XNA FrameworkUniqueConstraint(DataColumn, Boolean)Initializes a new instance of the UniqueConstraint class with the DataColumn to constrain, and a value specifying whether the constraint is a primary key.
Public methodSupported by the XNA FrameworkUniqueConstraint(DataColumn[], Boolean)Initializes a new instance of the UniqueConstraint class with an array of DataColumn objects to constrain, and a value specifying whether the constraint is a primary key.
Public methodSupported by the XNA FrameworkUniqueConstraint(String, DataColumn)Initializes a new instance of the UniqueConstraint class with the specified name and DataColumn.
Public methodSupported by the XNA FrameworkUniqueConstraint(String, DataColumn[])Initializes a new instance of the UniqueConstraint class with the specified name and array of DataColumn objects.
Public methodSupported by the XNA FrameworkUniqueConstraint(String, DataColumn, Boolean)Initializes a new instance of the UniqueConstraint class with the specified name, the DataColumn to constrain, and a value specifying whether the constraint is a primary key.
Public methodSupported by the XNA FrameworkUniqueConstraint(String, DataColumn[], Boolean)Initializes a new instance of the UniqueConstraint class with the specified name, an array of DataColumn objects to constrain, and a value specifying whether the constraint is a primary key.
Public methodSupported by the XNA FrameworkUniqueConstraint(String, String[], Boolean)Initializes a new instance of the UniqueConstraint class with the specified name, an array of DataColumn objects to constrain, and a value specifying whether the constraint is a primary key.
Top

  NameDescription
Protected propertySupported by the XNA Framework_DataSetInfrastructure. Gets the DataSet to which this constraint belongs. (Inherited from Constraint.)
Public propertySupported by the XNA FrameworkColumnsGets the array of columns that this constraint affects.
Public propertySupported by the XNA FrameworkConstraintNameThe name of a constraint in the ConstraintCollection. (Inherited from Constraint.)
Public propertySupported by the XNA FrameworkExtendedPropertiesGets the collection of user-defined constraint properties. (Inherited from Constraint.)
Public propertySupported by the XNA FrameworkIsPrimaryKeyGets a value indicating whether or not the constraint is on a primary key.
Public propertySupported by the XNA FrameworkTableGets the table to which this constraint belongs. (Overrides Constraint.Table.)
Top

  NameDescription
Protected methodSupported by the XNA FrameworkCheckStateForPropertyInfrastructure. Gets the DataSet to which this constraint belongs. (Inherited from Constraint.)
Public methodSupported by the XNA FrameworkEqualsCompares this constraint to a second to determine if both are identical. (Overrides Object.Equals(Object).)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeGets the hash code of this instance of the UniqueConstraint object. (Overrides Object.GetHashCode().)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSetDataSetSets the constraint's DataSet. (Inherited from Constraint.)
Public methodSupported by the XNA FrameworkToStringGets the ConstraintName, if there is one, as a string. (Inherited from Constraint.)
Top

The UniqueConstraint is enforced on a single column (or columns) to ensure that a primary key value is unique.

Constraints are not enforced unless the EnforceConstraints property is set to true.

When the a DataSet is merged with a second DataSet, constraints are not enforced until all merges are completed.

The following example adds a UniqueConstraint to a DataSet and sets the EnforceConstraints property to true.

Public Sub CreateDataTable()
    Dim dataTable As DataTable = New DataTable
    dataTable.Columns.Add("CustomerID", Type.GetType("System.String"))
    dataTable.Columns.Add("CompanyName", Type.GetType("System.String"))

    Dim uniqueConstraint As UniqueConstraint = _
      New UniqueConstraint("CustConstraint", _
      New DataColumn() {dataTable.Columns("CustomerID"), _
      dataTable.Columns("CompanyName")})

    dataTable.Constraints.Add(uniqueConstraint)
End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is suitable for multithreaded read operations. You must synchronize any write operations.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft