Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

UniqueConstraint Constructor

.NET Framework 1.1

Initializes a new instance of the UniqueConstraint class.

Overload List

Initializes a new instance of the UniqueConstraint class with the specified DataColumn.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(DataColumn)
[C#] public UniqueConstraint(DataColumn);
[C++] public: UniqueConstraint(DataColumn*);
[JScript] public function UniqueConstraint(DataColumn);

Initializes a new instance of the UniqueConstraint class with the given array of DataColumn objects.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(DataColumn())
[C#] public UniqueConstraint(DataColumn[]);
[C++] public: UniqueConstraint(DataColumn*[]);
[JScript] public function UniqueConstraint(DataColumn[]);

Initializes a new instance of the UniqueConstraint class with the DataColumn to constrain, and a value specifying whether the constraint is a primary key.

[Visual Basic] Public Sub New(DataColumn, Boolean)
[C#] public UniqueConstraint(DataColumn, bool);
[C++] public: UniqueConstraint(DataColumn*, bool);
[JScript] public function UniqueConstraint(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.

[Visual Basic] Public Sub New(DataColumn(), Boolean)
[C#] public UniqueConstraint(DataColumn[], bool);
[C++] public: UniqueConstraint(DataColumn*[], bool);
[JScript] public function UniqueConstraint(DataColumn[], Boolean);

Initializes a new instance of the UniqueConstraint class with the specified name and DataColumn.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, DataColumn)
[C#] public UniqueConstraint(string, DataColumn);
[C++] public: UniqueConstraint(String*, DataColumn*);
[JScript] public function UniqueConstraint(String, DataColumn);

Initializes a new instance of the UniqueConstraint class with the specified name and array of DataColumn objects.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, DataColumn())
[C#] public UniqueConstraint(string, DataColumn[]);
[C++] public: UniqueConstraint(String*, DataColumn[]);
[JScript] public function UniqueConstraint(String, DataColumn[]);

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.

[Visual Basic] Public Sub New(String, DataColumn, Boolean)
[C#] public UniqueConstraint(string, DataColumn, bool);
[C++] public: UniqueConstraint(String*, DataColumn*, bool);
[JScript] public function UniqueConstraint(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.

[Visual Basic] Public Sub New(String, DataColumn(), Boolean)
[C#] public UniqueConstraint(string, DataColumn[], bool);
[C++] public: UniqueConstraint(String*, DataColumn[], bool);
[JScript] public function UniqueConstraint(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.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, String(), Boolean)
[C#] public UniqueConstraint(string, string[], bool);
[C++] public: UniqueConstraint(String*, String*[], bool);
[JScript] public function UniqueConstraint(String, String[], Boolean);

Example

[Visual Basic, C#, C++] The following example creates a DataTable with two columns, and adds a new UniqueConstraint to the ConstraintCollection.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the UniqueConstraint constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub MakeTableWithUniqueConstraint()
    Dim myTable As New DataTable("myTable")
    Dim idCol As New DataColumn("id", System.Type.GetType("System.Int32"))
    'idCol.Unique=true;
    Dim NameCol As New DataColumn("Name", System.Type.GetType("System.String"))
    'NameCol.Unique=true;
    myTable.Columns.Add(idCol)
    myTable.Columns.Add(NameCol)
    AddUniqueConstraint(myTable)
    ' Add one row to the table.
    Dim myRow As DataRow
    myRow = myTable.NewRow()
    myRow("id") = 1
    myRow("Name") = "John"
    myTable.Rows.Add(myRow)
    ' Display the table in a DataGrid control. 
    dataGrid1.DataSource = myTable
    ' Try to add an identical row.
    Try
        myRow = myTable.NewRow()
        myRow("id") = 1
        myRow("Name") = "John"
        myTable.Rows.Add(myRow)
    Catch e As Exception
      Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
      log.Source = "My Application"
      log.WriteEntry(e.ToString())
      Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
    End Try
End Sub 'MakeTableWithUniqueConstraint

Private Sub AddUniqueConstraint(myTable As DataTable)
    ' Create the DataColumn array.
    Dim myColumns(2) As DataColumn
    myColumns(0) = myTable.Columns("id")
    myColumns(1) = myTable.Columns("Name")
    Dim myUniqueConstraint As UniqueConstraint
    myUniqueConstraint = New UniqueConstraint("idNameConstraint", myColumns)
    myTable.Constraints.Add(myUniqueConstraint)
End Sub 'AddUniqueConstraint

[C#] 
private void MakeTableWithUniqueConstraint(){
    DataTable myTable = new DataTable("myTable");
    DataColumn idCol = new DataColumn("id",System.Type.GetType("System.Int32"));
    //idCol.Unique=true;
    DataColumn NameCol = new DataColumn("Name", System.Type.GetType("System.String"));
    //NameCol.Unique=true;
    myTable.Columns.Add(idCol);
    myTable.Columns.Add(NameCol);
    AddUniqueConstraint(myTable);
    // Add one row to the table.
    DataRow myRow;
    myRow = myTable.NewRow();
    myRow["id"] = 1;
    myRow["Name"] = "John";
    myTable.Rows.Add(myRow);
    // Display the table in a DataGrid control. 
    dataGrid1.DataSource=myTable;
    // Try to add an identical row.
    try
    {
        myRow = myTable.NewRow();
        myRow["id"] = 1;
        myRow["Name"] = "John";
        myTable.Rows.Add(myRow);
    }
    catch(Exception e)
       {
         System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
         log.Source = "My Application";
         log.WriteEntry(e.ToString());
         Console.WriteLine("Exception of type {0} occurred.", e.GetType());
       }
 }
 private void AddUniqueConstraint (DataTable myTable){
    // Create the DataColumn array.
    DataColumn[] myColumns = new DataColumn[2];
    myColumns[0] = myTable.Columns["id"];
    myColumns[1] = myTable.Columns["Name"];
    UniqueConstraint myUniqueConstraint;
    myUniqueConstraint = new UniqueConstraint("idNameConstraint", myColumns);
    myTable.Constraints.Add(myUniqueConstraint);
 }

[C++] 
private:
void MakeTableWithUniqueConstraint(){
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* idCol = new DataColumn(S"id",System::Type::GetType(S"System.Int32"));
    //idCol.Unique=true;
    DataColumn* NameCol = new DataColumn(S"Name", System::Type::GetType(S"System.String"));
    //NameCol.Unique=true;
    myTable->Columns->Add(idCol);
    myTable->Columns->Add(NameCol);
    AddUniqueConstraint(myTable);
    // Add one row to the table.
    DataRow* myRow;
    myRow = myTable->NewRow();
    myRow->Item[S"id"] = __box(1);
    myRow->Item[S"Name"] = S"John";
    myTable->Rows->Add(myRow);
    // Display the table in a DataGrid control. 
    dataGrid1->DataSource=myTable;
    // Try to add an identical row.
    try
    {
        myRow = myTable->NewRow();
        myRow->Item[S"id"] = __box(1);
        myRow->Item[S"Name"] = S"John";
        myTable->Rows->Add(myRow);
    }
    catch(Exception* e)
       {
         System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
         log->Source = S"My Application";
         log->WriteEntry(e->ToString());
         Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
       }
 }

 void AddUniqueConstraint (DataTable* myTable){
    // Create the DataColumn array.
    DataColumn* myColumns[] = new DataColumn*[2];
    myColumns->Item[0] = myTable->Columns->Item[S"id"];
    myColumns->Item[1] = myTable->Columns->Item[S"Name"];
    UniqueConstraint* myUniqueConstraint;
    myUniqueConstraint = new UniqueConstraint(S"idNameConstraint", myColumns);
    myTable->Constraints->Add(myUniqueConstraint);
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

UniqueConstraint Class | UniqueConstraint Members | System.Data Namespace

Show:
© 2015 Microsoft