tags:

views:

47

answers:

2

is this possible instead of an incrementing ID number, I have GUID in the code instead?

+1  A: 

Certainly, check here:

http://msdn.microsoft.com/en-us/library/system.data.datatable.primarykey(VS.71).aspx

   // Create a new DataTable and set two DataColumn objects as primary keys.
   DataTable myTable = new DataTable();
   DataColumn[] keys = new DataColumn[1];
   DataColumn myColumn = new DataColumn();
   myColumn.DataType = System.Type.GetType("System.Guid");
   myColumn.ColumnName= "ID";
   // Add the column to the DataTable.Columns collection.
   myTable.Columns.Add(myColumn);
   keys[0] = myColumn;
   // Set the PrimaryKeys property to the array.
   myTable.PrimaryKey = keys;
CSharper
the PrimaryKey attribute is a DataColumn[], im getting an errror from your code.
Martin Ongtangco
sorry, give that a shot. It's an array because you can set a multi-column primary key.
CSharper
Excellent, it works! thanks man!
Martin Ongtangco
A: 

One option is to normally have the value of the primary key as NEWID() to assign GUID to a variable declared as the uniqueidentifier data type

The other option is to have sequential GUIDs using this NEWSEQUENTIALID() function available in sqlserver. However MSDN states: If privacy is a concern, do not use this function. It is possible to guess the value of the next generated GUID and, therefore, access data associated with that GUID.

Example of SqlQuery using newid()

CREATE TABLE cust
(
 CustomerID uniqueidentifier NOT NULL
   DEFAULT newid(),
 Company varchar(30) NOT NULL,
 ContactName varchar(60) NOT NULL, 
 Address varchar(30) NOT NULL, 
 City varchar(30) NOT NULL,
 StateProvince varchar(10) NULL,
 PostalCode varchar(10) NOT NULL, 
 CountryRegion varchar(20) NOT NULL, 
 Telephone varchar(15) NOT NULL,
 Fax varchar(15) NULL
)
Kavitesh Singh