comparing byte arrays

E

eoghan.kenny

Hi,

I need to compare two timestamp columns in sql server and see which one
is greater. (i can tell if they are not equal buts not enough for this
requirement).

A timestamp value is unique in a database and always increments. A
non-null timestamp is type Binary(8) in sql server and I pass it to a
byte array in C#. To compare the two byte arrays I use
BitConverter.ToUInt64, as this converts an 8 byte array to a 64 bit
integer. I can then compare the two integers to see which is greater.

Is this method of comparing byte arrays correct ?

Thanks.
 
T

Truong Hong Thi

The SqlBinary class provides many methods/operators (like equality
operator and Equals method) which you can use.
SqlDataReader.GetSqlBinary() return SqlBinary.
 
E

ek1

thats a good tip about the sqlbinary type, i hadn't thought of that.
However it doesn't seem to help me determine which timestamp or byte
array is the greater, only if they are or are not equal.

one other way i have seen this done is to compare the timestamps using
SQL, but I want to to this in my C# program.
 
T

Truong Hong Thi

However it doesn't seem to help me determine which timestamp or byte
array is the greater
It does, in fact:
if (binary1 > binary2) {}
You can even do like this:
if (binary1 >= binary2) {}
Same for ==, <, <=. SqlBinary has overloads all of these operators.
Just see its member in MSDN.

Of course, you can also use binary.CompareTo(binary2)
 

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

Top