简单管用就行,谢啦!

解决方案 »

  1.   

    我的OLE数据是一个文件。我在file.read就出问题了。
    CFile file;
    file.Open(pathname,CFile::modeRead);
    ULONG filelenth=file.GetLength();//读取文件长度 SAFEARRAYBOUND szbuffer[1];
    SAFEARRAY FAR *psa;
    _variant_t varChunk;
            szbuffer->lLbound =0;
    szbuffer->cElements =filelenth;
    file.Read(szbuffer,filelenth);
            file.Close();
      

  2.   

    BOOL xxxxxx::SaveData()
    {
    try
    {
    //读取文件
    char *lpBuffer;
    char *temp;
    DWORD m_nFileLen;
    CFile f;
    if(!f.Open("你的文件名",CFile::modeRead))
    {
    AfxMessageBox("打开文件失败.");
    return FALSE;
    }
    m_nFileLen = f.GetLength();
    lpBuffer = new char[m_nFileLen];
    if(lpBuffer == NULL)
    {
    AfxMessageBox("分配文件缓冲区失败.");
    return FALSE;
    }
    if (f.ReadHuge(lpBuffer,m_nFileLen)!= m_nFileLen)
    {
    AfxMessageBox("读文件到内存出错");
    return FALSE;
    }
    f.Close();
    //打开记录集
    _RecordsetPtr m_recordsetSave;
    m_recordsetSave.CreateInstance(__uuidof(Recordset));
    _bstr_t SQLWord_t=(_bstr_t)csSelTable;
    m_recordsetSave->Open(SQLWord_t,
    _variant_t((IDispatch *)PLMConnection,TRUE),
    adOpenDynamic,adLockOptimistic,
    adCmdText);
    _variant_t var;
    SAFEARRAY *psa; 
    SAFEARRAYBOUND rgsabound[1];
    temp=lpBuffer;
    if(lpBuffer)
    {
    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = m_nFileLen;
    psa = SafeArrayCreate(VT_UI1, 1, rgsabound); //创建SAFEARRAY对象
    for (long i = 0; i < (long)m_nFileLen; i++)
    {
    SafeArrayPutElement (psa, &i,temp++);
    //将lpBuffer指向的二进制数据保存到SAFEARRAY对象psa中 
    }
    var.vt = VT_ARRAY | VT_UI1;//将var的类型设置为BYTE类型的数组
    var.parray = psa; //为var变量赋值 
    m_recordsetSave->GetFields()->GetItem((_bstr_t)csFieldName)->AppendChunk(var); 
    //加入BLOB类型的数据

    m_recordsetSave->Update(); //保存我们的数据到库中 
    delete []lpBuffer; }
    catch (_com_error &e)
    {
    CString thismsg;
    thismsg="保存出错:";
    thismsg+=e.ErrorMessage();
    AfxMessageBox(thismsg); 
    return FALSE;
    }
    return TRUE;
    }
      

  3.   


    long AddFileToDatabase(CQSTreeItem *pitem, CString filename)
    {
    CQsofficeApp *papp=(CQsofficeApp*)AfxGetApp();
    CString tablename=pitem->tablename;
    try
    {
    m_commandptr.CreateInstance (__uuidof(Command));
    m_pUserSet.CreateInstance (__uuidof(Recordset));

    //然后添加到tid????中去
    CFile file;
    if (!file.Open (filename ,CFile::modeRead))
    return 2;
    DWORD   filesize=file.GetLength ();
    char *m_pBuffer;
    m_pBuffer=new char[filesize+1];
    if (file.ReadHuge (m_pBuffer,filesize)!=filesize)
    return 1;
    char* pwritebase=m_pBuffer;      
    VARIANT varBlob;
    SAFEARRAY* psa;
    SAFEARRAYBOUND safebound[1];    
    if(pwritebase)
    {
    safebound[0].lLbound =0;
    safebound[0].cElements =filesize;
    psa=SafeArrayCreate(VT_UI1,1,safebound);
    for (long i=0;i<(long)filesize;i++)
    SafeArrayPutElement(psa,&i,pwritebase++);
    varBlob.vt =VT_ARRAY | VT_UI1;
    varBlob.parray =psa;
    }
    sqltext="insert into ";
    sqltext+=(_bstr_t)tablename;
    sqltext+=" (id,parentid,bedelete,data) values (?,?,0,?)";
    m_commandptr.CreateInstance (__uuidof(Command));
    m_commandptr->ActiveConnection =papp->m_pConnection ;
    m_commandptr->CommandText =sqltext;
    m_commandptr->CommandType =adCmdText;
    m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("tableitemid",adInteger,adParamInput,4,COleVariant((short)tableitemid)));
    m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("parentid",adInteger,adParamInput,4,COleVariant((short)pitem->parentid )));
    m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("data",/*adLongVarBinary*/adVarBinary,adParamInput,filesize+1,varBlob));
    m_commandptr->Execute (NULL,NULL,adCmdUnknown); delete []m_pBuffer;
    }
    catch(...)
    {
    // delete []m_pBuffer;
    DisplayAdoError();
    }   return 0;
    }