DataTable.PrimaryKey DataTable.PrimaryKey DataTable.PrimaryKey DataTable.PrimaryKey Property

定義

取得或設定資料行的陣列,這些資料行是做為資料表 (Data Table) 之主索引鍵。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); };
[System.ComponentModel.TypeConverter(typeof(System.Data.PrimaryKeyTypeConverter))]
[System.Data.DataSysDescription("DataTablePrimaryKeyDescr")]
public System.Data.DataColumn[] PrimaryKey { get; set; }
member this.PrimaryKey : System.Data.DataColumn[] with get, set
Public Property PrimaryKey As 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. 第二個範例示範如何設定的主鍵資料行DataTableThe 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.
    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;
}
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 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

備註

資料表的主鍵必須是唯一的,才能識別資料表中的記錄。The primary key of a table must be unique to identify the record in the table. 也可以有一個主鍵由兩個或多個資料行組成的資料表。It's also possible to have a table with a primary key made up of two or more columns. 當單一資料行不能包含足夠的唯一值時,就會發生這種情況。This occurs when a single column can't contain enough unique values. 例如,兩個數據行的主鍵可能是由 "FirstName" 和 "LastName" 資料行所組成。For example, a two column primary key might consist of a "FirstName" and "LastName" 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.

適用於

另請參閱