NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Building a Primary Key for a Table

With the basic structure of the Customers table, we can clean up the table's definition. The first task is to define a primary key. In a database, it's a general rule that every table has a column, or group of columns, that uniquely identify each row in the table. This column (or columns) is called the primary key. For our Customers table, the "CustID" column will be the primary key.

To define a primary key, we'll set some additional properties on the "CustID" column to make it work like a primary key in a database. The first property "AllowNull" specifies whether a null value is allowed. As in a true database primary key, we set the property to false.

 workColumn.AllowNull = false;

Setting the Unique property specifies whether the values contained within the column must be unique. In other words, no two values in the column can be identical. Again, since this column will act like our primary key we'll set this value to true.

 workColumn.Unique = true;

Now that the column is configured like a primary key, it can be assigned to the DataTable object's PrimaryKey property. The primary key is a column, or set of columns, containing values that are unique for each row in the table. For the Customers table, the primary key will be the CustID column:

[VB]
Dim myColArray As DataColumn()
myColArray = workTable.Columns("CustID")
workTable.PrimaryKey = myColArray
[C#]
workTable.PrimaryKey = new DataColumn[] {worktable["CustID"]};

If we were going to define additional columns as part of the primary key, it would look link this:

[VB]
myColArray(0) = workTable.Columns("Col1")
myColArray(1) = workTable.Columns("Col2")
workTable.PrimaryKey = myColArray
[C#]
workTable.PrimaryKey = new DataColumn[] {worktable["Col1"], worktable["Col2"]};