目的是将图片存入SQL SERVER数据库中的一image类型字段,结果程序运行到update时报错,异常抛出.不知道程序哪里出问题. strcpy(szCommand,"SELECT * FROM T_DBIndex");
try
{
lstate = m_pRecordset->State;
if(lstate == adStateOpen)
m_pRecordset->Close();
m_pRecordset->Open((_bstr_t)szCommand,
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText); Fields *pFields = m_pRecordset->GetFields();
Field *pField = pFields->GetItem("other3");
// pField->get_ActualSize(&nLenth);
CFile imagefile;
if(0 == imagefile.Open("C:\\WINNT\\Blue Lace 16.bmp",CFile::modeRead))
return;
long nLength = imagefile.GetLength();
BYTE *pbuf = NULL;
BYTE *pBufEx = NULL;
pbuf = new BYTE[nLength];
pBufEx = pbuf;
if(pbuf == NULL)
return; //allocate memory error;
imagefile.Read(pbuf,nLength); //read the file into memory //build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); for (long j = 0; j < nLength; j++)
SafeArrayPutElement (psa, &j, pBufEx++);
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
m_pRecordset->AddNew();
pField->AppendChunk(varBLOB);
m_pRecordset->Update(); //这句一执行就出错了,跳到异常抛出语句去了
delete []pbuf; }
catch(...)
{
AfxMessageBox("E");
}
try
{
lstate = m_pRecordset->State;
if(lstate == adStateOpen)
m_pRecordset->Close();
m_pRecordset->Open((_bstr_t)szCommand,
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText); Fields *pFields = m_pRecordset->GetFields();
Field *pField = pFields->GetItem("other3");
// pField->get_ActualSize(&nLenth);
CFile imagefile;
if(0 == imagefile.Open("C:\\WINNT\\Blue Lace 16.bmp",CFile::modeRead))
return;
long nLength = imagefile.GetLength();
BYTE *pbuf = NULL;
BYTE *pBufEx = NULL;
pbuf = new BYTE[nLength];
pBufEx = pbuf;
if(pbuf == NULL)
return; //allocate memory error;
imagefile.Read(pbuf,nLength); //read the file into memory //build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); for (long j = 0; j < nLength; j++)
SafeArrayPutElement (psa, &j, pBufEx++);
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
m_pRecordset->AddNew();
pField->AppendChunk(varBLOB);
m_pRecordset->Update(); //这句一执行就出错了,跳到异常抛出语句去了
delete []pbuf; }
catch(...)
{
AfxMessageBox("E");
}
数据库的数据类型对不对?另外,你SafeArrayCreate后,还需要释放内存,你光释放pbuf是不够的,SafeArrayCreate会申请新的空间,并不是简单的引用pBufEx的地址,不过这与你的Update语句出错应该没什么关系。