Connecting Tech Pros Worldwide Help | Site Map

comparing byte arrays

  #1  
Old March 9th, 2006, 09:45 AM
eoghan.kenny@gmail.com
Guest
 
Posts: n/a
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.

  #2  
Old March 9th, 2006, 10:35 AM
Truong Hong Thi
Guest
 
Posts: n/a

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.

  #3  
Old March 9th, 2006, 12:45 PM
ek1
Guest
 
Posts: n/a

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.

  #4  
Old March 9th, 2006, 01:15 PM
Truong Hong Thi
Guest
 
Posts: n/a

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)

  #5  
Old March 9th, 2006, 03:05 PM
ek1
Guest
 
Posts: n/a

re: comparing byte arrays


yes you're correct, thanks for your help !

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing byte arrays Peter answers 22 November 22nd, 2005 10:19 AM
Comparing byte arrays Peter answers 0 November 22nd, 2005 10:18 AM
Comparing byte arrays Peter answers 11 July 21st, 2005 02:43 PM
Comparing byte arrays Peter answers 11 July 21st, 2005 02:33 PM