UniqueConstraint-Konstruktor (String, DataColumn )
Initialisiert eine neue Instanz der UniqueConstraint-Klasse mit dem angegebenen Namen und dem angegebenen Array von DataColumn-Objekten.
Namespace: System.Data
Assembly: System.Data (in system.data.dll)
Syntax
'Declaration
Public Sub New ( _
name As String, _
columns As DataColumn() _
)
'Usage
Dim name As String
Dim columns As DataColumn()
Dim instance As New UniqueConstraint(name, columns)
public UniqueConstraint (
string name,
DataColumn[] columns
)
public:
UniqueConstraint (
String^ name,
array<DataColumn^>^ columns
)
public UniqueConstraint (
String name,
DataColumn[] columns
)
public function UniqueConstraint (
name : String,
columns : DataColumn[]
)
Parameter
- name
Der Name der Einschränkung.
- columns
Das Array von einzuschränkenden DataColumn-Objekten.
Beispiel
Im folgenden Beispiel wird eine DataTable mit zwei Spalten erstellt, und der ConstraintCollection wird eine neue UniqueConstraint hinzugefügt.
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
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);
}
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
UniqueConstraint-Klasse
UniqueConstraint-Member
System.Data-Namespace