注册 登录
编程论坛 VC++/MFC

ADO操作SQL Server的image类型数据,为什么读出来结果有错?

发布于 2011-11-09 01:33, 473 次点击
程序代码:

void CADOTest1AppDlg::OnButton1()
{
    // TODO: Add your control notification handler code here

    CoInitialize(NULL);
    _ConnectionPtr pConnection(__uuidof(Connection));
    _RecordsetPtr pRs(__uuidof(Recordset));

    pConnection->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=MyDB;Data Source=PC";
    pConnection->Open("","","",NULL);

    CFile imagefile;
    if(imagefile.Open("ReadMe.txt",CFile::modeRead)==0)
    {
        MessageBox("can't not open the file !");
        return ;
    }

    _variant_t varChunk;
//    HRESULT hr;
    BYTE* pbuf;
    int nLength=imagefile.GetLength();
    pbuf = new BYTE[nLength+2];
    if(pbuf==NULL)
        return;                             //allocate memory error;
    imagefile.Read(pbuf,nLength);          //read the file into memory
   
    BYTE *pBufEx;
    pBufEx = pbuf;
    //build a SAFFERRAY
    SAFEARRAY* psa;
    SAFEARRAYBOUND rgsabound[1];
    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = nLength;
    psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
   
    for (long i = 0; i < nLength; i++)
        SafeArrayPutElement (psa, &i, pBufEx++);
    VARIANT varBLOB;
    varBLOB.vt = VT_ARRAY | VT_UI1;
    varBLOB.parray = psa;
   
    pRs->Open("txdb",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);  //Open a Table
   
    pRs->AddNew();        
    pRs->Fields->GetItem("tx")->AppendChunk(varBLOB);        
    pRs->Update();
}

void CADOTest1AppDlg::OnButton2()
{
    // TODO: Add your control notification handler code here

    CoInitialize(NULL);
    _ConnectionPtr pConnection(__uuidof(Connection));
    _RecordsetPtr pRs(__uuidof(Recordset));

    pConnection->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=MyDB;Data Source=PC";
    pConnection->Open("","","",NULL);

    pRs->Open("txdb",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);

    VARIANT varBLOB;
    long lDataLength = pRs->Fields->GetItem("tx")->ActualSize;
    varBLOB = pRs->GetFields()->GetItem("tx")->GetChunk(lDataLength);
    if(varBLOB.vt == (VT_ARRAY | VT_UI1))        
    {
        BYTE *pBuf = NULL;   
        pBuf = (BYTE*)GlobalAlloc(GMEM_FIXED,lDataLength);
        SafeArrayAccessData(varBLOB.parray,(void **)pBuf);

        char tmpPath[_MAX_PATH+1];
        GetTempPath(_MAX_PATH,tmpPath);
        CString strFileName = "temp.txt";
        //strFileName = tmpPath+strFileName;MessageBox(strFileName);
        
        CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite);
        LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)pBuf);
        outFile.WriteHuge(buffer,lDataLength);
        GlobalUnlock((HGLOBAL)pBuf);
        outFile.Close();           
        SafeArrayUnaccessData (varBLOB.parray);
    }   
}


为什么我存进去数据以后读出来的文件和原来的不一样了?
0 回复
1