8-byte value as primary key?




A colleague of mine is trying to use an eight-byte array as the primary key into an ADO.NET DataTable, but he's getting exceptions thrown when he attempts to set the appropriate column as the primary key. Here's his sample:

public static void ByteArrayKey () {
DataTable dt = new DataTable();
dt.Columns.Add (new DataColumn("bytes",typeof(System.Byte[])));

DataRow dr = dt.NewRow();
dr["bytes"] = new System.Byte[] {0,1};
dr = dt.NewRow();
dr["bytes"] = new System.Byte[] {1,0};
try {
dt.PrimaryKey = new DataColumn[]{dt.Columns[0]};
System.Console.WriteLine("Set primaryKey as byte array");
} catch (System.ArgumentException e) {
System.Console.WriteLine("Could not set primaryKey as byte array: {0}",


This code will result in the exception being thrown. There *IS* a SqlDbType.Binary which is what we really want here, but I can't seem to find the appropriate System.Type to suggest as the DataColumn type to avoid the exception.

Anyone have ideas?

/s/ James

Val Mazur

Hi James,

I believe you cannot use binary datatype as a primary key. Actually you
cannot use any BLOB-type field as a primary key. What about using int64
datatype? It is an 8-byte long, but not sure if this is what you need.
Another potential way is to create 8 columns of byte datatype and group them
into one primary key

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question