Connecting Tech Pros Worldwide Forums | Help | Site Map

comparing byte arrays

eoghan.kenny@gmail.com
Guest
 
Posts: n/a
#1: Mar 9 '06
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.


Truong Hong Thi
Guest
 
Posts: n/a
#2: Mar 9 '06

re: comparing byte arrays


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

ek1
Guest
 
Posts: n/a
#3: Mar 9 '06

re: comparing byte arrays


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.

Truong Hong Thi
Guest
 
Posts: n/a
#4: Mar 9 '06

re: comparing byte arrays


>However it doesn't seem to help me determine which timestamp or byte[color=blue]
>array is the greater[/color]
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)

ek1
Guest
 
Posts: n/a
#5: Mar 9 '06

re: comparing byte arrays


yes you're correct, thanks for your help !

Closed Thread


Similar C# / C Sharp bytes