Using GetBytes() on an Access database

G

Guest

I have a simple Web Form application that needs to interact with an existing
MS Access database. In general, this is working well.

I've connected to the DB via ADO.NET (OleDB) and am able to read all
"normal" fields correctly (via DataReader).

The problem arises when I try to read an "OLE Object" typed field. The
field actually contains binary data (a JPEG image) but when I use GetBytes()
to populate the byte[], the data is munged.

It looks like it's been translated into a Unicode representation. The data
is exactly twice as long as it should be and most of the extra data consists
of zeros immediatley after the real byte value (i.e. little endian short).

This would be easy to work around except that some of the data is altered
more severly. For instance, the value 0x80 is converted to 0x20AC.

Obviously, I missing something regarding data translation; a flag specifying
binary or some such. Any ideas?

Thanks,

-Rob


private void test()
{
FileStream os = new FileStream(fn, FileMode.Create);
BinaryWriter wr = new BinaryWriter(os);

int size = 10*1024;
int offset = 0;
byte[] buffer = new byte[size];
long res = size;
long sum = 0;
while(true)
{
res = reader.GetBytes(photoord, offset, buffer, 0, size);
if(res == 0) break;

wr.Write(buffer, 0, (int) res);
offset += size;
}
wr.Close();
}
 
G

Guest

I've been reading everything I can find on the web to answer this question
but nothing suggests that the data should be anything but unaltered binary.

Since Unicode mappings are not one-to-one I can't just reverse the process;
I need to prevent the remapping from occuring in the first place.

If anyone has any ideas, please let me know.

Thanks,

-Rob
 
G

Guest

I decided to, again, review the MS Access DAO code that uses this same
database and ensure, via the documentation, that the functions used to store
and retrieve the data did not do anything unusual. According to the docs,
they don't.

The code does use strings but the docs are very specific that strings use
8-bit representations. And, since the data read is 8-bit and the data
written is 8-bit, I had no reason to question this.

But, since I'm running out of options, I decided to verify this for myself.
Using a hex editor, I found an image in the database and found that the data
is stored as 16-bit entities.

Thus, I am getting back the unaltered binary data and it is Access that
munges the data when added/retrieved from the database.

Does anyone know what mapping Access uses? It doesn't appear to be plain
Unicode as I've tried using the Encoding class to map from Unicode to UTF8 (I
even tried byte swapping the data first) (I can't use the ASCII converter as
it limits the result to 7-bits). My best guess is that it uses a Window's
codepage but I'm not sure how to work with that encoding in C#.

-Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top