IsPrimaryKey [UniqueKeyConstraint only]
bool isPrimaryKey = Constraint.IsPrimaryKey; Constraint.IsPrimaryKey = isPrimaryKey;
Indicates whether the UniqueKeyConstraint
represents the primary key for the table.
You can specify that a UniqueKeyConstraint
should
represent the primary key when creating the
UniqueKeyConstraint
by setting the
IsPrimaryKey
property to true
.
The following code snippet creates a
UniqueKeyConstraint
that represents a primary key
defined on a first and last name column:
// Create an array with the two columns. DataColumn[] cols = new DataColumn[] {dt.Columns["LastName"], dt.Columns["FirstName"]}; // Create the UniqueConstraint object to represent the primary key. UniqueConstraint uc = new UniqueConstraint("FullName", cols, true); // Add the UniqueConstraint to the table. dt.Constraints.Add(uc);
A primary key is created automatically when you use the
DataAdapter.FillSchema( )
method, as long as there
is at least one unique column in the query. There can be only one
primary key on a DataTable
at a time. If you
attempt to set a second primary key on a table, the original primary
key is downgraded to a unique column.