DataTable.PrimaryKey プロパティ
このデータ テーブルの主キーとして機能する列の配列を取得または設定します。
Public Property PrimaryKey As DataColumn ()
[C#]
public DataColumn[] PrimaryKey {get; set;}
[C++]
public: __property DataColumn* get_PrimaryKey();public: __property void set_PrimaryKey(DataColumn*[]);
[JScript]
public function get PrimaryKey() : DataColumn[];
public function set PrimaryKey(DataColumn[]);
プロパティ値
DataColumn オブジェクトの配列。
例外
例外の種類 | 条件 |
---|---|
DataException | キーが外部キーです。 |
解説
テーブルの主キーは、テーブル内のレコードを識別するために一意である必要があります。テーブルの主キーを 2 つ以上の列でも構成できます。単一の列では一意の値を格納しきれない場合にこれを行います。たとえば、主キーを "FirstName" 列と "LastName" 列の 2 列で構成できます。主キーを複数の列で構成できるため、 PrimaryKey プロパティは DataColumn オブジェクトの配列で構成されます。
使用例
[Visual Basic, C#, C++] DataGrid に表示される DataTable の主キー列を返す方法を次の例に示します。2 番目の例は、 DataTable の主キー列の設定方法を示します。
Private Sub GetPrimaryKeys(myTable As DataTable)
' Create the array for the columns.
Dim colArr() As DataColumn
colArr = myTable.PrimaryKey
' Get the number of elements in the array.
Console.WriteLine("Column Count: " & colArr.Length.ToString())
Dim i As Integer
For i = 0 To colArr.GetUpperBound(0)
Console.WriteLine(colArr(i).ColumnName & colArr(i).DataType.ToString())
Next i
End Sub
Private Sub SetPrimaryKeys()
' Create a new DataTable and set two DataColumn objects as primary keys.
Dim myTable As DataTable = new DataTable()
Dim keys(2) As DataColumn
Dim myColumn As DataColumn
' Create column 1.
myColumn = New DataColumn()
myColumn.DataType = System.Type.GetType("System.String")
myColumn.ColumnName= "FirstName"
' Add the column to the DataTable.Columns collection.
myTable.Columns.Add(myColumn)
' Add the column to the array.
keys(0) = myColumn
' Create column 2 and add it to the array.
myColumn = New DataColumn()
myColumn.DataType = System.Type.GetType("System.String")
myColumn.ColumnName = "LastName"
myTable.Columns.Add(myColumn)
' Add the column to the array.
keys(1) = myColumn
' Set the PrimaryKeys property to the array.
myTable.PrimaryKey = keys
End Sub
[C#]
private void GetPrimaryKeys(DataTable myTable){
// Create the array for the columns.
DataColumn[] colArr;
colArr = myTable.PrimaryKey;
// Get the number of elements in the array.
Console.WriteLine("Column Count: " + colArr.Length);
for(int i = 0; i < colArr.Length; i++){
Console.WriteLine(colArr[i].ColumnName + colArr[i].DataType);
}
}
private void SetPrimaryKeys(){
// Create a new DataTable and set two DataColumn objects as primary keys.
DataTable myTable = new DataTable();
DataColumn[] keys = new DataColumn[2];
DataColumn myColumn;
// Create column 1.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
myColumn.ColumnName= "FirstName";
// Add the column to the DataTable.Columns collection.
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[0] = myColumn;
// Create column 2 and add it to the array.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.String");
myColumn.ColumnName = "LastName";
myTable.Columns.Add(myColumn);
// Add the column to the array.
keys[1] = myColumn;
// Set the PrimaryKeys property to the array.
myTable.PrimaryKey = keys;
}
[C++]
private:
void GetPrimaryKeys(DataTable* myTable){
// Create the array for the columns.
DataColumn* colArr[];
colArr = myTable->PrimaryKey;
// Get the number of elements in the array.
Console::WriteLine(S"Column Count: {0}", __box(colArr->Length));
for(int i = 0; i < colArr->Length; i++){
Console::WriteLine(S"{0}{1}", colArr[i]->ColumnName, colArr[i]->DataType);
}
}
private:
void SetPrimaryKeys(){
// Create a new DataTable and set two DataColumn objects as primary keys.
DataTable* myTable = new DataTable();
DataColumn* keys[] = new DataColumn*[2];
DataColumn* myColumn;
// Create column 1.
myColumn = new DataColumn();
myColumn->DataType = System::Type::GetType(S"System.String");
myColumn->ColumnName= S"FirstName";
// Add the column to the DataTable.Columns collection.
myTable->Columns->Add(myColumn);
// Add the column to the array.
keys[0] = myColumn;
// Create column 2 and add it to the array.
myColumn = new DataColumn();
myColumn->DataType = System::Type::GetType(S"System.String");
myColumn->ColumnName = S"LastName";
myTable->Columns->Add(myColumn);
// Add the column to the array.
keys[1] = myColumn;
// Set the PrimaryKeys property to the array.
myTable->PrimaryKey = keys;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
DataTable クラス | DataTable メンバ | System.Data 名前空間 | DataColumn | PrimaryKey | DataColumnCollection