casting BLOB to a string

  • Thread starter Thread starter martin
  • Start date Start date
M

martin

Hi,

I have blob field in my database.
I am retrieveing the field through a stored procedure and the execute scalar
method.

I would like to cast resulting "byte" field to a string.
I am using the syntax
 
It's not as simple as a cast. You will get a byte[] from the database, and
you can create a new MemoryStream and then pull data from that MemoryStream,
using tools such as the BinaryFormatter to serialize and deserialize from
that stream.
 
Chris said:
It's not as simple as a cast. You will get a byte[] from the database, and
you can create a new MemoryStream and then pull data from that MemoryStream,
using tools such as the BinaryFormatter to serialize and deserialize from
that stream.

Or if the byte array blob is truly just a string of characters something
like:

string s = System.Text.Encoding.Unicode.GetString( blob);

would do the trick - changing the encoding from Unicode to whatever is
appropriate if the blob is not Unicode characters.
 
Back
Top