我想从SQL SERVER中读图片字段.代码如下:
(1) long fileLength = m_pRecordset->GetFields()->GetItem("photo")->ActualSize;
(2) variant_t varBLOB;
(3) varBLOB = m_pRecordset->GetFields()->GetItem(_variant_t("photo"))->GetChunk(fileLength);
(4) char* fileBuffer = new char[fileLength];
(5) SAFEARRAY *psa;
(6) SAFEARRAYBOUND rgsabound[1];
(7) rgsabound[0].lLbound = 0;
(8) rgsabound[0].cElements = fileLength;
(9) psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
(10) for(long index=0;index<fileLength;index++)
{
(11) SafeArrayGetElement(varBLOB.parray,&index,fileBuffer+index);
}
以前认为variant_t是VARIANT的封装,就把它们等同看待.可是调试时发现第2行如果换成VARIANT后,fileBuffer中就读不到数据.这是为什么啊???恳请大家指教
(1) long fileLength = m_pRecordset->GetFields()->GetItem("photo")->ActualSize;
(2) variant_t varBLOB;
(3) varBLOB = m_pRecordset->GetFields()->GetItem(_variant_t("photo"))->GetChunk(fileLength);
(4) char* fileBuffer = new char[fileLength];
(5) SAFEARRAY *psa;
(6) SAFEARRAYBOUND rgsabound[1];
(7) rgsabound[0].lLbound = 0;
(8) rgsabound[0].cElements = fileLength;
(9) psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
(10) for(long index=0;index<fileLength;index++)
{
(11) SafeArrayGetElement(varBLOB.parray,&index,fileBuffer+index);
}
以前认为variant_t是VARIANT的封装,就把它们等同看待.可是调试时发现第2行如果换成VARIANT后,fileBuffer中就读不到数据.这是为什么啊???恳请大家指教
GetChunk()的返回值是_variant_t,当然varBLOB要设成_variant_t的类型的了
至于为什么换成VARIANT后,读出来的数据无法正常写到fileBuffer,一个偷懒的回答是,_variant_t与VARIANT的定义不一样。