我用javascript+vc,现在客户要求能保留原来的word文档。
我执行  select * from dali where contains(MyImage,'J老师') 得到的结果是二进制的,这怎么解决?

解决方案 »

  1.   

    // VC6 读取sql server 图片
    // 使用了class CADORecordset
    CString strSql = "select layerimage from EMAP_LAYER where imageid=1";
    CADORecordset recordset;
    if (recordset.Open(m_adoDb.GetActiveConnection(),strSql)
     && !recordset.IsEof())
    {
    CADOFieldInfo fldInfo;
    recordset.GetFieldInfo("layerimage",&fldInfo);
    char *pBmpBuf = new char [fldInfo.m_lSize];
    recordset.GetChunk("layerimage",pBmpBuf);
    recordset.Close(); CClientDC dc(this);
    BITMAPFILEHEADER bmfh;
    memcpy(&bmfh,pBmpBuf,sizeof(BITMAPFILEHEADER));
    BITMAPINFO *pbmi = (BITMAPINFO *)(pBmpBuf + sizeof(BITMAPFILEHEADER));
    BITMAPINFOHEADER *pbmih = (BITMAPINFOHEADER *)pbmi;
    m_hBitmap = ::CreateDIBitmap( // 构造DDB位图
    dc, // handle to DC
    pbmih, // bitmap data
    CBM_INIT, // initialization option
    (char *)(pBmpBuf+bmfh.bfOffBits), // initialization data
    pbmi, // color-format data
    DIB_RGB_COLORS ); // color-data usage delete [] pBmpBuf;
    }//显示图片很简单,用 BitBlt()就可以,参考msdn
      

  2.   

    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);    
     }
      

  3.   

    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);    
     } 
      

  4.   


    在VC中调用WORD(显示,修改,存盘,运行宏)    ZHENG017(原作) (1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"office"&nbsp; 
    (2)选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持,并且选中active document container 
    其它都为默认
    (3)在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 
    在Office目录中选中Microsoft Word 97/2000 
    类型库Word8.olb或Word9.olb,选中application,document,_document。单击ok
    (4)给COfficeCntrItem添加一方法。GetIDispatch()
    其源码如下:
    ASSERT_VALID(this);
    ASSERT(m_lpObject != NULL);
    LPUNKNOWN lpUnk = 
    m_lpObject;
    Run();
    LPOLELINK lpOleLink = NULL;
    if 
    (m_lpObject->;QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) ==NOERROR)
    {
    ASSERT(lpOleLink != NULL);
    lpUnk = NULL; 
    if (lpOleLink->;GetBoundSource(&;lpUnk) != NOERROR)

    TRACE0("Warning: Link is not connected!\n"); 
    lpOleLink->;Release();
    return NULL;} 
    ASSERT(lpUnk != NULL);
    }
    LPDISPATCH lpDispatch = NULL; 
    if (lpUnk->;QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)!=NOERROR)
    {
    TRACE0("Warning: does not 
    support IDispatch!\n");
    return NULL;
    }&nbsp; ASSERT(lpDispatch != NULL);
    return lpDispatch; 
    (5)。在officeView.h添加#include "msword8.h"
    (6)。修改void COfficeView::OnInsertObject(),源码如下:
    &nbsp;BeginWaitCursor();
    &nbsp;COfficeCntrItem* pItem = NULL;
    &nbsp;TRY
    &nbsp;{ &nbsp;// Create new item connected to this document.
    &nbsp;COfficeDoc* pDoc = GetDocument();
    &nbsp;ASSERT_VALID(pDoc);
    &nbsp;pItem = new COfficeCntrItem(pDoc);
    &nbsp;ASSERT_VALID(pItem);
    &nbsp; &nbsp;
    &nbsp;&nbsp;// Initialize the item from the dialog data.
    &nbsp;&nbsp;/*&nbsp;if(!dlg.CreateItem(pItem))
    &nbsp;&nbsp;AfxThrowMemoryException();&nbsp; // any exception will do
    &nbsp;&nbsp;ASSERT_VALID(pItem);*/
    &nbsp;&nbsp;CLSID clsid; //
    &nbsp;&nbsp;if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid))) 
    AfxThrowMemoryException(); 
    if(!pItem->CreateNewItem(clsid))
    /*(!pItem->CreateFromFile (filename,clsid)) */
    AfxThrowMemoryException();
    ASSERT_VALID(pItem);  pItem->Activate (OLEIVERB_SHOW,this);
    &nbsp;&nbsp;ASSERT_VALID(pItem);
    &nbsp;&nbsp;m_pSelection = pItem;&nbsp;&nbsp; // set selection to last inserted item
    &nbsp;&nbsp;&nbsp;&nbsp;pDoc->UpdateAllViews(NULL);
    &nbsp;&nbsp;&nbsp;&nbsp;// As an arbitrary user interface design, this sets the selection
    &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp; to the lastitem inserted.
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;// TODO: reimplement selection as appropriate for your application
    &nbsp;&nbsp;
    &nbsp;&nbsp;m_pSelection = pItem;&nbsp;&nbsp; // set selection to last inserted item
    &nbsp;&nbsp;pDoc->UpdateAllViews(NULL);
    &nbsp;&nbsp;}
    &nbsp;&nbsp;CATCH(CException,e)
    &nbsp;&nbsp;{
    &nbsp;&nbsp;if (pItem != NULL)
    &nbsp;&nbsp;{
    &nbsp;&nbsp;ASSERT_VALID(pItem);
    &nbsp;&nbsp;pItem->Delete();
    &nbsp;&nbsp;}
    &nbsp;&nbsp;AfxMessageBox(IDP_FAILED_TO_CREATE);
    &nbsp;&nbsp;}
    &nbsp;&nbsp;END_CATCH
    &nbsp;EndWaitCursor();
    &nbsp;
    &nbsp;
    (7)重载ID—FILE—SAVE,
    void COfficeView::OnFileSave() 
    {
    // TODO: Add your command handler code here
    TRY
    {
    LPDISPATCH lpDisp; lpDisp = m_pSelection->GetIDispatch(); 
    &nbsp;&nbsp;&nbsp; Documents docs;
    &nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp; _Application app;
    &nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp; _Document mydoc;
    &nbsp;&nbsp;&nbsp; Documents my;
    &nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;mydoc.AttachDispatch (lpDisp,TRUE);
    &nbsp;&nbsp;app=mydoc.GetApplication ();
    &nbsp;&nbsp;/*&nbsp;("Macro3");*/&nbsp;&nbsp;&nbsp;&nbsp; 
    mydoc.Activate ();
    BOOL password=mydoc.GetHasPassword ();
    mydoc.SetPassword ("love");
    password=mydoc.GetHasPassword ();
    COleVariant vFalse((short)FALSE);
    mydoc.SaveAs (COleVariant("c:\\love.doc"),vFalse,vFalse,COleVariant(""),vFalse,
    COleVariant(""),vFalse,vFalse,vFalse,vFalse,vFalse); }
    CATCH(CException,e)
    &nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp; }
    END_CATCH } bulid,click insert object,and edit ,and save.maybe run macro.