convert SqlBinary to Byte[]

  • Thread starter Thread starter megan
  • Start date Start date
M

megan

I can easily get my byte array into the SqlBinary Sql servr field.
getting it out again is more difficult.

SqlBinary myBin = myBinaryObject; // object from SQL server field.
byte[] b= myBin ;

is not allowed.
 
megan said:
I can easily get my byte array into the SqlBinary Sql servr field.
getting it out again is more difficult.

SqlBinary myBin = myBinaryObject; // object from SQL server field.
byte[] b= myBin ;

According to the docs, the conversion operator from SqlBinary to byte[]
is defined as 'explicit', which means you must explicitly do the cast:

byte[] b = (byte[])myBin;

"If a conversion operation can cause exceptions or lose information,
.... [it should be marked] explicit. This prevents the compiler from
silently invoking the conversion operation with possibly unforeseen
consequences."
 
Hi Megan,

A SqlBinary has to Byte[] Conversion static function, so
you should be able to write:
byte[] myBytes=(byte[]) myBin;
I can easily get my byte array into the SqlBinary Sql servr field.
getting it out again is more difficult.

SqlBinary myBin = myBinaryObject; // object from SQL server field.
byte[] b= myBin ;

is not allowed.

It's not allowed, because C# is type safe language.
And that is a realy good thing.

Cheers & HTH
Marcin
 
Back
Top