1
Answer

C# string data and SQL 2000 binary data

Photo of Administrator

Administrator

22y
7.4k
1
I have a string-representation of a GUID being passed into my c# web service, and I want to look this value up in my SQL 2000 table. However, my GUIDS in the database are stored as binary. How can I compare these two values to find a match in the table? I tried SQL convert, cast, but they didn't work. Are there any C# conversion functions I can use? The two values I would like to compare look similar to: string GUID: 9F1F7BF37997604DB88249E9FFB2AD66 -- in C# binary GUID: 0x9F1F7BF37997604DB88249E9FFB2AD66 -- in DB Many thanks! Aaron

Answers (1)

2
Photo of Administrator
Tech Writer 2.2k 1.6m 22y
Hi Herbia53, Are you getting the value back as a SqlBinary type (one of the SqlTypes) in your DataSet? If so, this is converted by C# in a byte[] array. To convert from a byte array to a string, you can use the AscIIEncoding method GetString shown below: Converts a specified range of elements in an array of bytes to a String. [C#] [Serializable] public override string GetString( byte[] bytes, int byteIndex, int byteCount ); byteIndex - The index of the first position in bytes to convert. byteCount - The number of bytes to convert. Return Value - A String containing the values of the range in bytes from byteIndex to byteIndex + byteCount decoded as an ASCIIEncoding. -Mike G.