DataTable.PrimaryKey プロパティ

定義

このデータ テーブルの主キーとして機能する列の配列を取得または設定します。Gets or sets an array of columns that function as primary keys for the data table.

public:
 property cli::array <System::Data::DataColumn ^> ^ PrimaryKey { cli::array <System::Data::DataColumn ^> ^ get(); void set(cli::array <System::Data::DataColumn ^> ^ value); };
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))]
public System.Data.DataColumn[] PrimaryKey { 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
[<System.ComponentModel.TypeConverter(typeof(System.Data.PrimaryKeyTypeConverter))>]
member this.PrimaryKey : System.Data.DataColumn[] with get, set
Public Property PrimaryKey As DataColumn()

プロパティ値

DataColumn[]

DataColumn オブジェクトの配列。An array of DataColumn objects.

属性

例外

キーが外部キーです。The key is a foreign key.

最初の例は、に表示されているの主キー列を返す方法を示して DataTable DataGrid います。The first example shows how to return the primary key columns for a DataTable displayed in a DataGrid. 2番目の例では、の主キー列を設定する方法を示し DataTable ます。The second example demonstrates how to set the primary key columns for a DataTable.

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

注釈

テーブル内のレコードを識別するには、テーブルの主キーが一意である必要があります。The primary key of a table must be unique to identify the record in the table. また、2つ以上の列で構成される主キーを持つテーブルを作成することもできます。It's also possible to have a table with a primary key made up of two or more columns. これは、1つの列に十分な一意の値を含めることができない場合に発生します。This occurs when a single column can't contain enough unique values. たとえば、2つの列の主キーが "OrderNumber" 列と "ProductID" 列で構成されている場合があります。For example, a two column primary key might consist of an "OrderNumber" and "ProductID" column. 主キーは複数の列で構成できるので、プロパティは PrimaryKey オブジェクトの配列で構成され DataColumn ます。Because primary keys can be made up of more than one column, the PrimaryKey property consists of an array of DataColumn objects.

適用対象

こちらもご覧ください