Search a DataTable on a byte[] field

G

Guest

I am using a DataTable (dt) based on a SQL Server table containing a column of type varbinary(50) named descriptor. The DataTable column for this field is base64Binary, which seems to map to System.byte[]. I then defined a DataView (dv) with dv.Sort="descriptor". I find that if I have a byte[] variable bArray, then dv.FindRows(bArray) always matches all the rows of dv and dv.Find(bArray) always returns 0 (unless dt.Rows.Count=0). I don't understand this behavior

I can do a manual search by using
for(int i=0; i<dt.Rows.Count; i++)
if ((new SqlBinary(byte[] dt.Rows["descriptor"]))==(new SqlBinary(bArray))
..

But I would like to use a DataView. Is there any way to do this

John Kane
 
C

Cowboy

If you want to test like so:

if(ds.Table[0].Rows["BinaryFieldName"] == arrayName)

you need to either cast out the entire array as a string or overload the
Equals so you can compare contents.

Currently, you have array1 sitting at 0xFFFFFFF (Cleveland) and array2
sitting a 0xAAAAAAA (Newark) and you are asking (with the statement above)
are these two identical. So, the answer is "NO, because one is in Cleveland
and the other in Newark. Other than that, they are identical." You have to
get rid of the "that" part of the question (location) and compare contents.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
JJKane said:
I am using a DataTable (dt) based on a SQL Server table containing a
column of type varbinary(50) named descriptor. The DataTable column for this
field is base64Binary, which seems to map to System.byte[]. I then defined a
DataView (dv) with dv.Sort="descriptor". I find that if I have a byte[]
variable bArray, then dv.FindRows(bArray) always matches all the rows of dv
and dv.Find(bArray) always returns 0 (unless dt.Rows.Count=0). I don't
understand this behavior.
I can do a manual search by using:
for(int i=0; i<dt.Rows.Count; i++) {
if ((new SqlBinary(byte[] dt.Rows["descriptor"]))==(new SqlBinary(bArray)))
...
}
But I would like to use a DataView. Is there any way to do this?

John Kane
 
G

Guest

This isn't the question. I know that I can do a row-by-row comparision using s SqlBinary cast. My problem is: How can I do this using a DataView
John Kan

----- Cowboy wrote: ----

If you want to test like so

if(ds.Table[0].Rows["BinaryFieldName"] == arrayName

you need to either cast out the entire array as a string or overload th
Equals so you can compare contents

Currently, you have array1 sitting at 0xFFFFFFF (Cleveland) and array
sitting a 0xAAAAAAA (Newark) and you are asking (with the statement above
are these two identical. So, the answer is "NO, because one is in Clevelan
and the other in Newark. Other than that, they are identical." You have t
get rid of the "that" part of the question (location) and compare contents

--
Gregory A. Beame
MVP; MCP: +I, SE, SD, DB

***********************************************
Think Outside the Box
***********************************************
JJKane said:
I am using a DataTable (dt) based on a SQL Server table containing
column of type varbinary(50) named descriptor. The DataTable column for thi
field is base64Binary, which seems to map to System.byte[]. I then defined
DataView (dv) with dv.Sort="descriptor". I find that if I have a byte[
variable bArray, then dv.FindRows(bArray) always matches all the rows of d
and dv.Find(bArray) always returns 0 (unless dt.Rows.Count=0). I don'
understand this behavior
I can do a manual search by using
for(int i=0; i<dt.Rows.Count; i++)
if ((new SqlBinary(byte[] dt.Rows["descriptor"]))==(ne SqlBinary(bArray))
..

But I would like to use a DataView. Is there any way to do this
 
S

Steven Cheng[MSFT]

Hi John,

I think Cowboy's suggestion is reasonable, the DataView's Find and FindRows
method only support simple type compare such as string, int , long ...
These types has their meaningful "==" operator or Equals method, so that
the DataView's Find and FindRows method can make use of it to search the
requested row. The byte[] type doesn't support the "==" or "Equals"
compare. For example
byte[] b1 = System.Text.Encoding.ASCII.GetBytes("test");
byte[] b2 = System.Text.Encoding.ASCII.GetBytes("test");

b1 == b2 or b1.Equals(b2) will always return false, that's why the DataView
can't search record via byte array field.

I think to change the Column type to string(encode it into base64string) or
just use the SqlBinary type as the DataTable's Column Type, these two types
all support the "==" and Equals() compare.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

Hi John,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
G

Guest

Setting the DataColumn data type to SqlBinary seems to do the trick
Thanks
John Kan

----- Steven Cheng[MSFT] wrote: ----

Hi John

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks

Regards

Steven Chen
Microsoft Online Suppor

Get Secure! www.microsoft.com/securit
(This posting is provided "AS IS", with no warranties, and confers no
rights.

Get Preview at ASP.NET whidbe
http://msdn.microsoft.com/asp.net/whidbey/default.asp
 

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