Problems with array<Byte>^

D

Dennis Wagner

Hi everyone!
I was playing a little with VC++ 2005 Express for evaluation purposes
(and out of curiosity) when I stumbled over some problems. I have a set
of binary data in a postgresql-table. I managed to insert the data via
the npgsql-data-provider. I can also read string and numeric values, but
when it comes to reading the data out of the db and writing it to disk I
fail.
I tried the following (I did this on another computer and type from mind
here so don't bother typos plz):

String^ statement = "SELECT data FROM jpegimages WHERE \"ID\" = " +
Convert::ToString(newObject->GetID()) + ";";
NpgsqlCommand^ command = gcnew NpgsqlCommand(statement, this->conn);
NpgsqlDataReader^ reader = command->ExecuteReader();
// I know there is only one row...
reader->Read();
if (reader[0] != IntPrt::Zero) {

array<Byte>^ bytes = (array<Byte>^)(reader[0]);
bufferedWriter->Write(bytes);

...

The code compiles without errors but when it is run I get "Cast not
valid" Exceptions at the "array<Bytes.."-line.
There isn't much info in the help about stuff like this. The docs for
the postgres-data-provider are written against c# (which I didn't intent
to use in this special case) where one can use a simple "byte[]"-construct.
Any help is highly appreicated.

regards...


....Dennis
 
D

Dennis Wagner

Hi there again!

I solved it. Anyone who's interested: Query the size of the data
together with the binary data (in postgres: "SELECT data,
octet_length(data) From...") and do the following:

int dataLength = Convert::ToInt32(reader["octet_length"]);
array<Byte>^ data = gcnew array<Byte>(dataLength);
reader->GetBytes(column, 0, data, 0, dataLength);

Where column is the index of the data-carrying column in the
sql-statement. In the above given mini-example: column = 0

Greetz...


....Dennis
 

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