PrimaryKey
DataColumn[] pka = DataTable.PrimaryKey; DataTable.PrimaryKey = pka;
Accesses the array of DataColumn
objects that make
up the primary key of the table.
The primary key for a table can be set by specifying an array of
DataColumn
objects from the table. The following
example shows how to create a primary key based on two columns:
// set the primary key based on two columns in the DataTable DataTable dt = new DataTable("MyTable"); dt.Columns.Add("PK_Field1", typeof(System.Int32)); dt.Columns.Add("PK_Field2", typeof(System.Int32)); // add other table columns dt.PrimaryKey = new DataColumn[] {dt.Columns["PK_Field1"], dt.Columns["PK_Field2"]};
To remove the primary key, simply set the primary key to
null
, as shown in the following example:
// remove the primary key dt.PrimaryKey = null;