type casting between _variant_t and bitmap

I'm trying to retrieve values from a database.

This part works perfectly;
mydatabase_names variable type is varchar so this conversion works free of problems

Also a column holds pictures of the people in the data base. this time I can't make type casting like below;

(Bitmap*)pRecordset->Fields->GetItem("mydatabase_names")->Value;        //  or

pRecordset->Fields->GetItem("mydatabase_names")->Value is a _variant_t object.
Also I couldn't find how to use this object's ChangeType() methods

I get compiler error that type cast 'cannot convert'

How can I retrieve and hold these pictures?
Who is Participating?
Daniel JungesConnect With a Mentor Commented:

parray is a member of class _variant_t  // defined in comutil.h

// field definition
ADODB::Fields* fields;

for(int index=0; index<fieldsCount; index++){
  ADODB::Field* field;
  fields->get_Item(_variant_t((long)index), &field);
You can't just cast blob data like that.
When you get to a field that you know contains blob data, you will need to first read teh blob data into a buffer and only then pack into into a bitmap. Unfortunately, there are no native functions to do that for you...
sinacetinerAuthor Commented:
ok, but are there any other way to read data from a database except using _recordsetptr if exists what kind of buffer do I have to use?
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Daniel JungesCommented:
the sample below shows how to read blobs from database:

_variant_t var_column_value;
field->get_Value(&var_column_value); // field is received from  Recordset

SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
long len = field->ActualSize;
//Create a safe array to store the array of BYTES  
rgsabound[0].cElements = len;
psa = SafeArrayCreate(VT_UI1,1,rgsabound);
_variant_t varChunk = field->GetChunk(len);
unsigned char* data;
SafeArrayAccessData( varChunk.parray, ( void **)&data );
// -----> now you can construct you bitmap here
SafeArrayUnaccessData( varChunk.parray );

sinacetinerAuthor Commented:
pardon I couldn't understand the type of field if it is _recordsetptr there is no get_Value method exits. And also where was the parray defined? Is parray means psa?(maybe wrongly you used like that)
While constructing bitmap How can I use safearray.
sinacetinerAuthor Commented:
excuse me this is my last question.  How can I convert safearray into stream
None of the constructors allow creating a bitmap or image with safearray
sinacetinerAuthor Commented:
bitmap *img;
I have tried SafeArrayAccessData( varChunk.parray, ( void **)&img );
but this time an exception occurs
Daniel JungesCommented:
unsigned char* data;
SafeArrayAccessData( varChunk.parray, ( void **)&data );

// Here you have access to the binary data to construct you img, you have to copy the buffer
// if you want to use the img after calling the function bellow.

SafeArrayUnaccessData( varChunk.parray );
sinacetinerAuthor Commented:
can you give an example all you said is true but I get exception.  I can understand
SafeArrayAccessData(varChunk.parray, ( void **)&data) gives start of the blobs object but after that by using Bitmap(data) isnt valid and also using Bitmap(*data) is nonsense. also I tried to use IO::Stream and Byte array but they arent also useless and causes exception.
After that I look earlier lines in my code. I think usage of
pCommand->CommandText = "Select pictures From Student"; //pcommand is defined as _Commandptr
is true.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.