DataTable.PrimaryKey 속성

정의

데이터 테이블에 대한 기본 키로 사용되는 열의 배열을 가져오거나 설정합니다.

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()

속성 값

DataColumn 개체의 배열입니다.

특성

예외

해당 키가 외래 키인 경우

예제

첫 번째 예제에서는 에 표시 되는 에 대 한 기본 키 열을 반환 하는 DataTable 방법을 보여 줍니다 DataGrid. 두 번째 예제에서는 의 기본 키 열을 설정하는 방법을 보여 줍니다 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

설명

테이블의 기본 키는 테이블의 레코드를 식별하기 위해 고유해야 합니다. 두 개 이상의 열로 구성된 기본 키가 있는 테이블을 가질 수도 있습니다. 단일 열에 충분한 고유 값을 포함할 수 없는 경우에 발생합니다. 예를 들어 두 열 기본 키는 "OrderNumber" 및 "ProductID" 열로 구성될 수 있습니다. 기본 키는 둘 이상의 열로 구성될 수 있으므로 속성은 PrimaryKey 개체 배열 DataColumn 로 구성됩니다.

적용 대상

추가 정보