sskaar07

I have a varbinary(max) field in my sql 2005 db that I am passing to a CLR sproc as a sqlbinary type. I need to convert this to a byte[] that I can use as a parameter being passed to a web service method.

I have tried casting it as a byte and then assigning it to a new instance of a byte [] to no avail. Any help here would be much appreciated. I am new to C# so this one is really stumping me :-(

Thanks




Re: .NET Framework Data Access and Storage convert sqlbinary to byte[]

Manuel Bauer

Hi

This code sample should work fine:

Code Block

while (reader.Read()) {
// Read the data from varbinary(max) column
byte[] binaryData = (byte[])reader.GetValue(0);
}

or this:

Code Block

while (reader.Read()) {
SqlBytes bytes = reader.GetSqlBytes(0);

byte[] byteArr = bytes.Value;

}

Please have a look at this article:

http://msdn2.microsoft.com/en-us/library/a1904w6t(VS.80).aspx

Manuel Bauer






Re: .NET Framework Data Access and Storage convert sqlbinary to byte[]

sskaar07

Hi Manuel,

Thank you very much for your reply :-)

Since I am not using a data reader, just passing in the vars from the sproc to the CLR should I just use:

byte[] wtsCoronadoString = (byte[])dbCoronadoString.Value;

Thanks again!

Sandy Skaar






Re: .NET Framework Data Access and Storage convert sqlbinary to byte[]

Manuel Bauer

Well, that's nice. Wish you a lot of fun with your byte-arrays.