• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            山寨:不是最好的,是最適合我們的!歡迎體驗山寨 中文版MSDN

            Blog @ Blog

            當華美的葉片落盡,生命的脈絡才歷歷可見。 -- 聶魯達

            常用鏈接

            統計

            積分與排名

            BBS

            Blog

            Web

            最新評論

            關于用VC進行圖像數據(二進制大對象)存儲數據庫的一點心得

            下面給出用VC,VB如何操作圖像文件存取數據庫的原碼,幫助一些還沒有掌握方法的朋友,也請這方面的高手多多指教。(均用ADO連接數據庫)

            1. VC把一個文件存入數據庫

            CFile imagefile;
              
            if(0 == imagefile.Open("d:\\user\\bmp.bmp",CFile::modeRead))
                 
            return;
              _RecordsetPtr pRs 
            = NULL;             
              _ConnectionPtr pConnection 
            = NULL;
              _variant_t varChunk;
              HRESULT hr;
              BYTE
            * pbuf;
              
            long 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;

              _bstr_t strCnn(
            "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER");    
                
            try
                
            {
                    
            //Open a connection
                    pConnection.CreateInstance(__uuidof(Connection));
                    hr 
            = pConnection->Open(strCnn,"","",NULL);   //Connect a DataBase
                    pRs.CreateInstance(__uuidof(Recordset));
                    pRs
            ->Open("CustomInfo",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);  //Open a Table
             
            //      pRs->AddNew();        
                    pRs->Fields->GetItem("Image")->AppendChunk(varBLOB);        
                    pRs
            ->Update();
                    pRs
            ->Close();
                    pConnection
            ->Close();
             }

                
            catch(_com_error &e)
                
            {
                    
            // Notify the user of errors if any.

                    _bstr_t bstrSource(e.Source());
                    _bstr_t bstrDescription(e.Description());
                    CString sError;
                    sError.Format(
            "Source : %s \n Description : %s\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
                    AfxMessageBox(sError);     
             }


             2. VC把數據庫中IMAGE字段取出存為文件
                _RecordsetPtr pRs = NULL;
                _ConnectionPtr pConnection 
            = NULL;
                _variant_t varChunk;
                HRESULT hr;
                VARIANT varBLOB;
                _bstr_t strCnn(
            "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER");    
                
            try
                
            {
                    
            //Open a connection
                    pConnection.CreateInstance(__uuidof(Connection));
                    hr 
            = pConnection->Open(strCnn,"","",NULL);        
                    pRs.CreateInstance(__uuidof(Recordset));
                    pRs
            ->Open("CustomInfo",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);
                   
            //read  data  
                   long lDataLength = pRs->Fields->GetItem("Image")->ActualSize;
                   varBLOB 
            = pRs->GetFields()->GetItem("Image")->GetChunk(lDataLength);
                  
            if(varBLOB.vt == (VT_ARRAY | VT_UI1))        
                 
            {
                        BYTE 
            *pBuf = NULL;   
                        pBuf 
            = (BYTE*)GlobalAlloc(GMEM_FIXED,lDataLength);
                        SafeArrayAccessData(varBLOB.parray,(
            void **)pBuf); 
                        
            //Build a File in Windows Temp Directory
                        char tmpPath[_MAX_PATH+1];
                        GetTempPath(_MAX_PATH,tmpPath);
                        CString strFileName 
            = "temp.bmp";
                        strFileName 
            = tmpPath+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);
                   }


                    pRs
            ->Close();
                    pConnection
            ->Close();
                 }

                
            catch(_com_error &e)
                
            {
                    
            // Notify the user of errors if any.
                    _bstr_t bstrSource(e.Source());
                    _bstr_t bstrDescription(e.Description());
                    CString sError;
                    sError.Format(
            "Source : %s \n Description : %s\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
                    AfxMessageBox(sError);     
             }
             
            轉自:http://dev.rdxx.com/VC/VCDB/2001-10/6/024749171.shtml

            posted on 2008-06-13 09:21 isabc 閱讀(691) 評論(0)  編輯 收藏 引用 所屬分類: 數據庫

            廣告信息(免費廣告聯系)

            中文版MSDN:
            歡迎體驗

            色欲久久久天天天综合网 | 人妻丰满AV无码久久不卡| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久人人爽人人爽人人片av高请 | 久久婷婷五月综合成人D啪| 亚洲精品高清国产一线久久| 久久99热精品| 亚洲一区精品伊人久久伊人| 精品久久久久香蕉网| 无码任你躁久久久久久老妇| 日韩精品久久无码中文字幕| 青青草国产精品久久久久| 久久天天躁狠狠躁夜夜avapp| 欧美亚洲国产精品久久蜜芽| 久久久亚洲AV波多野结衣| 国产精品永久久久久久久久久| 久久久久久久波多野结衣高潮| 国产成人99久久亚洲综合精品| 亚洲精品白浆高清久久久久久 | 天天躁日日躁狠狠久久| 久久强奷乱码老熟女网站| 99精品久久精品| 婷婷五月深深久久精品| 久久妇女高潮几次MBA| 久久久久亚洲AV无码专区桃色| 东京热TOKYO综合久久精品| 久久久一本精品99久久精品88| 国产精品久久新婚兰兰| 青青热久久国产久精品| 麻豆久久| 欧美精品乱码99久久蜜桃| 久久婷婷是五月综合色狠狠| 久久久精品国产亚洲成人满18免费网站| 亚洲国产精品无码久久一线| 久久精品国产99国产精品导航| 久久婷婷是五月综合色狠狠| 久久综合五月丁香久久激情| 久久久国产亚洲精品| 狠狠色婷婷久久一区二区| 久久青青草视频| 久久久久亚洲AV片无码下载蜜桃|