How do I bind a dataset to a blob?

  • Thread starter Thread starter David P. Donahue
  • Start date Start date
D

David P. Donahue

When I bind a dataset to a SQL query that contains a blob field it comes
back as a byte array. Is there any way to have it interpreted as a
string or somehow convert it to a string? I'm using the blob field to
store large amounts of text and I need to be able to interpret it as such.


Regards,
David P. Donahue
(e-mail address removed)
 
How is the text stored? ASCII? Unicode UTF-8? Unicode UTF-16?

Check out the System.Text.Encoding namespace for classes that deal with
transforming text.
 
The encoding really isn't the problem, as far as I can tell. Nothing
I've found in that namespace really does the trick. It's just that the
dataset is binding the column as a char[] or byte[] and I need it to be
a string. As it stands, anything that binds to the dataset and tries to
use the value in the column as a string returns the error "unknown
target conversion type."
 
I haven't done this myself, but just reading the documentation under
the Encoding.GetString() method....

Let's assume that the data is stored in your database as Unicode. So,
there's no conversion needed: it's already in the form used internally
by .NET strings.

Then you need to say:

System.Text.Encoding encoding = System.Text.Encoding.Unicode;
string blobString = encoding.GetString(blobByteArray);

This should transform the byte array to a string without any encoding
change (because .NET stores strings internally in Unicode).

Of course, if the byte array is not already encoded in Unicode then
this will produce garbage, but all you would have to do then is try
different encodings until you found the right one.
 
Back
Top