DataTable.PrimaryKey プロパティ
アセンブリ: 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 オブジェクトの配列。


テーブルの主キーは、テーブル内のレコードを識別するために一意である必要があります。テーブルの主キーを 2 つ以上の列でも構成できます。単一の列では一意の値を格納しきれない場合にこれを行います。たとえば、主キーを "FirstName" 列と "LastName" 列の 2 列で構成できます。主キーを複数の列で構成できるため、PrimaryKey プロパティは DataColumn オブジェクトの配列で構成されます。

DataGrid に表示される DataTable の主キー列を返す方法を次の例に示します。2 番目の例は、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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDataTable.PrimaryKey プロパティを検索する場合は、下記のリンクをクリックしてください。

- DataTable.PrimaryKey プロパティのページへのリンク