• <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 閱讀(690) 評論(0)  編輯 收藏 引用 所屬分類: 數據庫

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

            中文版MSDN:
            歡迎體驗

            国产精品美女久久久久av爽| 午夜视频久久久久一区| 久久综合给合久久国产免费| 久久综合九色综合网站| 久久精品视频网| 亚洲精品WWW久久久久久| 国产亚洲欧美精品久久久| 久久久久久亚洲精品无码| 日韩乱码人妻无码中文字幕久久| 亚洲国产精品一区二区久久| 女人高潮久久久叫人喷水| 国产精品一区二区久久| 奇米影视7777久久精品人人爽 | 91精品国产91久久| 亚洲精品tv久久久久久久久久| 久久夜色精品国产噜噜亚洲AV| 久久精品国产黑森林| 久久99久久99精品免视看动漫| 久久久久亚洲爆乳少妇无 | 色青青草原桃花久久综合| 大伊人青草狠狠久久| 久久精品无码免费不卡| 热re99久久6国产精品免费| 久久国产成人| 91精品国产91热久久久久福利| 一本一道久久综合狠狠老| 综合久久一区二区三区| 国产视频久久| 亚洲一区中文字幕久久| 国产精品久久久久久久| 久久亚洲中文字幕精品有坂深雪| 一级做a爰片久久毛片看看| 久久人人超碰精品CAOPOREN| 伊人久久综在合线亚洲2019| 欧美激情精品久久久久| 老色鬼久久亚洲AV综合| 精品国产VA久久久久久久冰| 久久精品国产亚洲AV无码娇色| 欧美一区二区三区久久综合| 亚洲午夜无码久久久久| 久久精品水蜜桃av综合天堂 |