Redigera

Dela via


UniqueConstraint Constructors

Definition

Initializes a new instance of the UniqueConstraint class.

Overloads

UniqueConstraint(DataColumn)

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

UniqueConstraint(DataColumn[])

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

UniqueConstraint(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.

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.

UniqueConstraint(String, DataColumn)

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

UniqueConstraint(String, DataColumn[])

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

UniqueConstraint(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.

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.

UniqueConstraint(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.

UniqueConstraint(DataColumn)

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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

public:
 UniqueConstraint(System::Data::DataColumn ^ column);
public UniqueConstraint (System.Data.DataColumn column);
new System.Data.UniqueConstraint : System.Data.DataColumn -> System.Data.UniqueConstraint
Public Sub New (column As DataColumn)

Parameters

column
DataColumn

The DataColumn to constrain.

Examples

The following example creates a new UniqueConstraint and assigns it to the ParentKeyConstraint property of a DataRelation.

Private Sub CreateUniqueConstraint(ByVal dataSetSuppliers As DataSet)
    Dim uniqueConstraint As UniqueConstraint

    ' Get the DataColumn of a table in a DataSet.
    Dim dataColumn As DataColumn
    dataColumn = dataSetSuppliers.Tables("Suppliers").Columns("SupplierID")

    ' Create the constraint.
    uniqueConstraint = New UniqueConstraint("supplierIdConstraint", dataColumn)

    ' Add the constraint to the ConstraintCollection of the DataTable.
    dataSetSuppliers.Tables("Suppliers").Constraints.Add(uniqueConstraint)
End Sub

See also

Applies to

UniqueConstraint(DataColumn[])

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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

public:
 UniqueConstraint(cli::array <System::Data::DataColumn ^> ^ columns);
public UniqueConstraint (System.Data.DataColumn[] columns);
new System.Data.UniqueConstraint : System.Data.DataColumn[] -> System.Data.UniqueConstraint
Public Sub New (columns As DataColumn())

Parameters

columns
DataColumn[]

The array of DataColumn objects to constrain.

See also

Applies to

UniqueConstraint(DataColumn, Boolean)

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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:
 UniqueConstraint(System::Data::DataColumn ^ column, bool isPrimaryKey);
public UniqueConstraint (System.Data.DataColumn column, bool isPrimaryKey);
new System.Data.UniqueConstraint : System.Data.DataColumn * bool -> System.Data.UniqueConstraint
Public Sub New (column As DataColumn, isPrimaryKey As Boolean)

Parameters

column
DataColumn

The DataColumn to constrain.

isPrimaryKey
Boolean

true to indicate that the constraint is a primary key; otherwise, false.

See also

Applies to

UniqueConstraint(DataColumn[], Boolean)

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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:
 UniqueConstraint(cli::array <System::Data::DataColumn ^> ^ columns, bool isPrimaryKey);
public UniqueConstraint (System.Data.DataColumn[] columns, bool isPrimaryKey);
new System.Data.UniqueConstraint : System.Data.DataColumn[] * bool -> System.Data.UniqueConstraint
Public Sub New (columns As DataColumn(), isPrimaryKey As Boolean)

Parameters

columns
DataColumn[]

An array of DataColumn objects to constrain.

isPrimaryKey
Boolean

true to indicate that the constraint is a primary key; otherwise, false.

See also

Applies to

UniqueConstraint(String, DataColumn)

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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

public:
 UniqueConstraint(System::String ^ name, System::Data::DataColumn ^ column);
public UniqueConstraint (string? name, System.Data.DataColumn column);
public UniqueConstraint (string name, System.Data.DataColumn column);
new System.Data.UniqueConstraint : string * System.Data.DataColumn -> System.Data.UniqueConstraint
Public Sub New (name As String, column As DataColumn)

Parameters

name
String

The name of the constraint.

column
DataColumn

The DataColumn to constrain.

Examples

The following example creates a new UniqueConstraint and assigns it to the ParentKeyConstraint property of a DataRelation.

Private Sub CreateUniqueConstraint(ByVal dataSetSuppliers As DataSet)
    Dim uniqueConstraint As UniqueConstraint

    ' Get the DataColumn of a table in a DataSet.
    Dim dataColumn As DataColumn
    dataColumn = dataSetSuppliers.Tables("Suppliers").Columns("SupplierID")

    ' Create the constraint.
    uniqueConstraint = New UniqueConstraint("supplierIdConstraint", dataColumn)

    ' Add the constraint to the ConstraintCollection of the DataTable.
    dataSetSuppliers.Tables("Suppliers").Constraints.Add(uniqueConstraint)
End Sub

See also

Applies to

UniqueConstraint(String, DataColumn[])

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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

public:
 UniqueConstraint(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ columns);
public UniqueConstraint (string? name, System.Data.DataColumn[] columns);
public UniqueConstraint (string name, System.Data.DataColumn[] columns);
new System.Data.UniqueConstraint : string * System.Data.DataColumn[] -> System.Data.UniqueConstraint
Public Sub New (name As String, columns As DataColumn())

Parameters

name
String

The name of the constraint.

columns
DataColumn[]

The array of DataColumn objects to constrain.

Examples

The following example creates a DataTable with two columns, and adds a new UniqueConstraint to the ConstraintCollection.

static private void MakeTableWithUniqueConstraint()
{
    // Create a DataTable with 2 DataColumns.
    DataTable dataTable = new DataTable("dataTable");
    DataColumn idColumn = new DataColumn(
        "id", System.Type.GetType("System.Int32"));
    DataColumn nameColumn = new DataColumn(
        "Name", System.Type.GetType("System.String"));
    dataTable.Columns.Add(idColumn);
    dataTable.Columns.Add(nameColumn);

    // Run procedure to create a constraint.
    AddUniqueConstraint(dataTable);

    // Add one row to the table.
    DataRow dataRow;
    dataRow = dataTable.NewRow();
    dataRow["id"] = 1;
    dataRow["Name"] = "John";
    dataTable.Rows.Add(dataRow);

    // Display the constraint name.
    Console.WriteLine(
        dataTable.Constraints[0].ConstraintName);

    // Try to add an identical row,
    // which throws an exception.
    try
    {
        dataRow = dataTable.NewRow();
        dataRow["id"] = 1;
        dataRow["Name"] = "John";
        dataTable.Rows.Add(dataRow);
    }
    catch (Exception ex)
    {
        Console.WriteLine(
            "Exception Type: {0}", ex.GetType());
        Console.WriteLine(
            "Exception Message: {0}", ex.Message);
    }
}
static private void AddUniqueConstraint(
    DataTable dataTable)
{
    // Create the DataColumn array.
    DataColumn[] dataColumns = new DataColumn[2];
    dataColumns[0] = dataTable.Columns["id"];
    dataColumns[1] = dataTable.Columns["Name"];

    // Create the constraint on both columns.
    UniqueConstraint uniqueConstraint =
        new UniqueConstraint("idNameConstraint", dataColumns);
    dataTable.Constraints.Add(uniqueConstraint);
}
Private Sub MakeTableWithUniqueConstraint()
    ' Create a DataTable with 2 DataColumns.
    Dim dataTable As New DataTable("dataTable")
    Dim idColumn As New DataColumn( _
        "ID", System.Type.GetType("System.Int32"))
    Dim nameColumn As New DataColumn( _
        "Name", System.Type.GetType("System.String"))
    dataTable.Columns.Add(idColumn)
    dataTable.Columns.Add(nameColumn)

    ' Run procedure to create a constraint.
    AddUniqueConstraint(dataTable)

    ' Add one row to the table.
    Dim dataRow As DataRow
    dataRow = dataTable.NewRow()
    dataRow("ID") = 1
    dataRow("Name") = "John"
    dataTable.Rows.Add(dataRow)

    ' Display the constraint name.
    Console.WriteLine(dataTable.Constraints(0).ConstraintName)

    ' Try to add an identical row,
    ' which throws an exception.
    Try
        dataRow = dataTable.NewRow()
        dataRow("ID") = 1
        dataRow("Name") = "John"
        dataTable.Rows.Add(dataRow)
    Catch ex As Exception
        Console.WriteLine("Exception Type: {0}", ex.GetType())
        Console.WriteLine("Exception Message: {0}", ex.Message)
    End Try
End Sub

Private Sub AddUniqueConstraint(ByVal dataTable As DataTable)
    ' Create the DataColumn array.
    Dim dataColumns(1) As DataColumn
    dataColumns(0) = dataTable.Columns("ID")
    dataColumns(1) = dataTable.Columns("Name")

    ' Create the constraint on both columns.
    Dim uniqueConstraint As UniqueConstraint = _
        New UniqueConstraint("idNameConstraint", dataColumns)
    dataTable.Constraints.Add(uniqueConstraint)
End Sub

See also

Applies to

UniqueConstraint(String, DataColumn, Boolean)

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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:
 UniqueConstraint(System::String ^ name, System::Data::DataColumn ^ column, bool isPrimaryKey);
public UniqueConstraint (string? name, System.Data.DataColumn column, bool isPrimaryKey);
public UniqueConstraint (string name, System.Data.DataColumn column, bool isPrimaryKey);
new System.Data.UniqueConstraint : string * System.Data.DataColumn * bool -> System.Data.UniqueConstraint
Public Sub New (name As String, column As DataColumn, isPrimaryKey As Boolean)

Parameters

name
String

The name of the constraint.

column
DataColumn

The DataColumn to constrain.

isPrimaryKey
Boolean

true to indicate that the constraint is a primary key; otherwise, false.

See also

Applies to

UniqueConstraint(String, DataColumn[], Boolean)

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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:
 UniqueConstraint(System::String ^ name, cli::array <System::Data::DataColumn ^> ^ columns, bool isPrimaryKey);
public UniqueConstraint (string? name, System.Data.DataColumn[] columns, bool isPrimaryKey);
public UniqueConstraint (string name, System.Data.DataColumn[] columns, bool isPrimaryKey);
new System.Data.UniqueConstraint : string * System.Data.DataColumn[] * bool -> System.Data.UniqueConstraint
Public Sub New (name As String, columns As DataColumn(), isPrimaryKey As Boolean)

Parameters

name
String

The name of the constraint.

columns
DataColumn[]

An array of DataColumn objects to constrain.

isPrimaryKey
Boolean

true to indicate that the constraint is a primary key; otherwise, false.

See also

Applies to

UniqueConstraint(String, String[], Boolean)

Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs
Source:
UniqueConstraint.cs

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:
 UniqueConstraint(System::String ^ name, cli::array <System::String ^> ^ columnNames, bool isPrimaryKey);
[System.ComponentModel.Browsable(false)]
public UniqueConstraint (string? name, string[]? columnNames, bool isPrimaryKey);
[System.ComponentModel.Browsable(false)]
public UniqueConstraint (string name, string[] columnNames, bool isPrimaryKey);
[<System.ComponentModel.Browsable(false)>]
new System.Data.UniqueConstraint : string * string[] * bool -> System.Data.UniqueConstraint
Public Sub New (name As String, columnNames As String(), isPrimaryKey As Boolean)

Parameters

name
String

The name of the constraint.

columnNames
String[]

An array of DataColumn objects to constrain.

isPrimaryKey
Boolean

true to indicate that the constraint is a primary key; otherwise, false.

Attributes

Remarks

This constructor is provided for design time support in the Visual Studio .NET environment. UniqueConstraint objects created by using this constructor must then be added to the collection via AddRange. Columns with the specified names must exist at the time the method is called, or if BeginInit has been called prior to calling this constructor, the columns with the specified names must exist at the time that EndInit is called.

See also

Applies to