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 类

其他资源

创建和使用 DataTables