다음을 통해 공유


DataTable.PrimaryKey 속성

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

네임스페이스: System.Data
어셈블리: System.Data(system.data.dll)

구문

‘선언
Public Property PrimaryKey As DataColumn()
‘사용 방법
Dim instance As DataTable
Dim value As DataColumn()

value = instance.PrimaryKey

instance.PrimaryKey = value
public DataColumn[] PrimaryKey { get; set; }
public:
property array<DataColumn^>^ PrimaryKey {
    array<DataColumn^>^ get ();
    void set (array<DataColumn^>^ value);
}
/** @property */
public DataColumn[] get_PrimaryKey ()

/** @property */
public void set_PrimaryKey (DataColumn[] value)
public function get PrimaryKey () : DataColumn[]

public function set PrimaryKey (value : DataColumn[])

속성 값

DataColumn 개체로 이루어진 배열입니다.

예외

예외 형식 조건

DataException

해당 키가 외래 키인 경우

설명

테이블의 레코드를 식별하려면 테이블의 기본 키는 고유해야 합니다. 테이블에 두 개 이상의 열로 구성된 기본 키를 포함할 수도 있습니다. 이것은 한 개의 열로는 충분한 고유 값을 포함할 수 없는 경우에 해당합니다. 예를 들어, 두 개의 열 기본 키는 "FirstName" 및 "LastName" 열로 구성될 수 있습니다. 기본 키는 두 개 이상의 열로 구성될 수 있으므로 PrimaryKey 속성은 DataColumn 개체의 배열로 구성됩니다.

예제

첫 번째 예제에서는 DataGrid에 표시되는 DataTable의 기본 키 열을 반환하는 방법을 보여 줍니다. 두 번째 예제에서는 DataTable의 기본 키 열을 설정하는 방법을 보여 줍니다.

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.ToString())
   Dim i As Integer
   For i = 0 To columns.GetUpperBound(0)
      Console.WriteLine(columns(i).ColumnName & columns(i).DataType.ToString())
   Next i
End Sub

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

   ' Create column 1.
   column = New DataColumn()
   column.DataType = System.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 = System.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
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.
    DataTable table = new DataTable();
    DataColumn[] keys = new DataColumn[2];
    DataColumn column;

    // Create column 1.
    column = new DataColumn();
    column.DataType = System.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 = System.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;
}

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

DataTable 클래스
DataTable 멤버
System.Data 네임스페이스
DataColumn 클래스
DataTable.PrimaryKey 속성
DataColumnCollection 클래스

기타 리소스

DataTable 작성 및 사용