我用ADO访问ORACLE数据库
   连接字符串:
CString strConnection = "Provider=OraOLEDB.Oracle.1;User ID="+ m_strUsername+";password="+ m_strPassword+";Data Source="+m_strDBName+";Persist Security Info=False";现在我要实现将一个文件存入数据库:
我写的代码如下:
CString strFileName="";
CFileDialog fdlg(TRUE, ".bmp", "", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_SHAREAWARE, "BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*||", NULL);
if(fdlg.DoModal()==IDOK)
{
strFileName = fdlg.GetPathName();
}
if(strFileName=="")
return;
VARIANT varBLOB;
SAFEARRAY *psy;
SAFEARRAYBOUND rgsabound[1];
char * pbuf, *pbufex; pbuf = NULL;
pbufex = NULL;
FILE* fp;
fp = fopen(strFileName,"rb");
if(fp == NULL)
{
CString strMsg;
strMsg.Format("打开文件:%s失败,请检查文件是否存在!",strFileName);
MessageBox(strMsg,"系统提示:",MB_OK|MB_ICONINFORMATION);
return;
}
int handle = fileno(fp);
long size = filelength(handle);//获取文件长度
pbuf = new char[size];
    pbufex = &pbuf[0];
    fseek(fp,0,0);
    _read(handle,pbufex,size);
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = size;
psy = SafeArrayCreate(VT_UI1,1,rgsabound);
for(long i=0;i<size;i++)
{
SafeArrayPutElement(psy,&i,pbufex++);
}
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psy;
_variant_t var;
    _RecordsetPtr rst;
rst.CreateInstance(__uuidof(Recordset));
CString  strSQL;
strSQL.Format("select test4.FNAME from test4");
if(pbufex)
{
try
{
        rst->Open(_bstr_t(strSQL), theApp.m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
    rst->AddNew();
rst->PutCollect("FNAME",(_variant_t)strFileName);
rst->GetFields()->GetItem("FCONTENT")->AppendChunk((_variant_t)varBLOB);
rst->Update();
}
catch(...)
{
CCommon common;
            AfxMessageBox(common.DisplayAdoError(theApp.m_pConnection));
pbufex = NULL;
if(pbuf)
delete[] pbuf;
if(rst!=NULL)
{
if(rst->State == adStateOpen)
rst->Close();

}
return;
}
AfxMessageBox("T");

pbufex = NULL;
if(pbuf)
delete[] pbuf;
if(rst!=NULL)
{
if(rst->State == adStateOpen)
rst->Close(); }
}当执行到rst->GetFields()->GetItem("FCONTENT")->AppendChunk((_variant_t)varBLOB);
的时候出现异常!
以上的代码我以前在ACCESS数据库中验证过是可用的。
请问ORACLE下该如何解决?
急用,大家帮帮忙.

解决方案 »

  1.   

    rgsabound[0].cElements = size;
    SIZE是指定了的。这段代码我以前在ACCESS是验证过的,但在ORACLE上确有问题...
      

  2.   

    问题已经找到
    SELECT 语句没有选择 FCONTENT字段!!!
    狂晕!谢谢大家的参与!
      

  3.   

    顺便说一句,对于blob字段的操作需要注意:如果数据库把blob字段设置为非空,你这样直接appendchunk可能会出问题,你可以试一下。最好是先执行一个insert 语句,给blob字段一个empty_blob的值,然后把这条记录select 出来再appendchunkcatch的时候改成(_com_error e)
    通过得到e.description()就知道什么错误了,容易查到原因