DataTable.PrimaryKey Eigenschaft

Definition

Ruft ein Array von Spalten ab, die als Primärschlüssel für die Datentabelle verwendet werden, oder legt dieses fest.

public:
 property cli::array <System::Data::DataColumn ^> ^ PrimaryKey { cli::array <System::Data::DataColumn ^> ^ get(); void set(cli::array <System::Data::DataColumn ^> ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Data.PrimaryKeyTypeConverter))]
public System.Data.DataColumn[] PrimaryKey { get; set; }
public System.Data.DataColumn[] PrimaryKey { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Data.PrimaryKeyTypeConverter))]
[System.Data.DataSysDescription("DataTablePrimaryKeyDescr")]
public System.Data.DataColumn[] PrimaryKey { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Data.PrimaryKeyTypeConverter))>]
member this.PrimaryKey : System.Data.DataColumn[] with get, set
member this.PrimaryKey : System.Data.DataColumn[] with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Data.PrimaryKeyTypeConverter))>]
[<System.Data.DataSysDescription("DataTablePrimaryKeyDescr")>]
member this.PrimaryKey : System.Data.DataColumn[] with get, set
Public Property PrimaryKey As DataColumn()

Eigenschaftswert

DataColumn[]

Ein Array von DataColumn-Objekten.

Attribute

Ausnahmen

Der Schlüssel ist ein Fremdschlüssel.

Beispiele

Im ersten Beispiel wird gezeigt, wie die Primärschlüsselspalten für eine DataTable in einer DataGridangezeigten . Im zweiten Beispiel wird veranschaulicht, wie die Primärschlüsselspalten für eine DataTablefestgelegt werden.

private void GetPrimaryKeys(DataTable table)
{
    // Create the array for the columns.
    DataColumn[] columns;
    columns = table.PrimaryKey;

    // Get the number of elements in the array.
    Console.WriteLine($"Column Count: {columns.Length}");
    for (int i = 0; i < columns.Length; i++)
    {
        Console.WriteLine($"{columns[i].ColumnName} {columns[i].DataType}");
    }
}

private void SetPrimaryKeys()
{
    // Create a new DataTable and set two DataColumn objects as primary keys.
    var table = new DataTable();
    var keys = new DataColumn[2];
    DataColumn column;

    // Create column 1.
    column = new DataColumn();
    column.DataType = Type.GetType("System.String");
    column.ColumnName= "FirstName";

    // Add the column to the DataTable.Columns collection.
    table.Columns.Add(column);

    // Add the column to the array.
    keys[0] = column;

    // Create column 2 and add it to the array.
    column = new DataColumn();
    column.DataType = Type.GetType("System.String");
    column.ColumnName = "LastName";
    table.Columns.Add(column);

    // Add the column to the array.
    keys[1] = column;

    // Set the PrimaryKeys property to the array.
    table.PrimaryKey = keys;
}
Private Sub GetPrimaryKeys(table As DataTable)
    ' Create the array for the columns.
    Dim columns As DataColumn()
    columns = table.PrimaryKey

    ' Get the number of elements in the array.
    Console.WriteLine($"Column Count: {columns.Length}")
    For i = 0 To columns.GetUpperBound(0)
        Console.WriteLine($"{columns(i).ColumnName} {columns(i).DataType}")
    Next
End Sub

Private Sub SetPrimaryKeys()
    ' Create a new DataTable and set two DataColumn objects as primary keys.
    Dim table As New DataTable()
    Dim keys(1) As DataColumn
    Dim column  As DataColumn

    ' Create column 1.
    column = New DataColumn()
    column.DataType = Type.GetType("System.String")
    column.ColumnName= "FirstName"

    ' Add the column to the DataTable.Columns collection.
    table.Columns.Add(column)
    ' Add the column to the array.
    keys(0) = column

    ' Create column 2 and add it to the array.
    column = New DataColumn()
    column.DataType = Type.GetType("System.String")
    column.ColumnName = "LastName"
    table.Columns.Add(column)

    ' Add the column to the array.
    keys(1) = column

    ' Set the PrimaryKeys property to the array.
    table.PrimaryKey = keys
End Sub

Hinweise

Der Primärschlüssel einer Tabelle muss eindeutig sein, um den Datensatz in der Tabelle zu identifizieren. Es ist auch möglich, eine Tabelle mit einem Primärschlüssel aus zwei oder mehr Spalten zu haben. Dies tritt auf, wenn eine einzelne Spalte nicht genügend eindeutige Werte enthalten kann. Ein primärer Schlüssel mit zwei Spalten kann z. B. aus einer Spalte "OrderNumber" und "ProductID" bestehen. Da Primärschlüssel aus mehreren Spalten bestehen können, besteht die PrimaryKey Eigenschaft aus einem Array von DataColumn Objekten.

Gilt für

Siehe auch