DataTable.PrimaryKey 屬性
本文內容
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定資料行的陣列,這些資料行是做為資料表 (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))]
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 所組成。
| 產品 | 版本 |
|---|---|
| .NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
| .NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
| .NET Standard | 2.0, 2.1 |