读书人

CRichEditCtrl和数据库交互!解决办法

发布时间: 2012-01-19 20:57:58 作者: rapoo

CRichEditCtrl和数据库交互!
怎样才能把 CRichEditCtrl 控件当前选中的或者全部的内容,包括格式化的文本,图形图像,Ole 对象等存储到数据库中,比如 Access 中,然后需要的时候又可以从数据库中读取显示到 CRichEditCtrl 中,我本来想生成一个 RTF 文件,然后把 RTF 文件存储到数据库中,但这样太占用空间,而且读取速度是不是也会跟不上啊,我想应该有直接存储和读取的方法的吧,先谢谢大家了!

[解决办法]
你这个问题问得返回太大了.
涉及到的东西真不少
[解决办法]
这个问题我也想知道,查了很多时间,最后只能用文件保存到Ole 对象,读取数据库的时候先在temp文件夹里生成rtf文件,再发rtf文件读取到RichEditView

C/C++ code
DWORD CALLBACK CENBView::MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)//读取{   CFile* pFile = (CFile*) dwCookie;   ASSERT_KINDOF(CFile,pFile);   *pcb = pFile->Read(pbBuff, cb);   return 0;}DWORD CALLBACK CENBView::MyStreamOutCallback(DWORD dwCookie,LPBYTE pbBuff, LONG cb, LONG *pcb)//输出{   CFile* pFile = (CFile*) dwCookie;   pFile->Write(pbBuff, cb);   *pcb = cb;   return 0;}BOOL CENBView::OnMySave(){    CMainFrame* m_pMf=(CMainFrame*)AfxGetApp()->GetMainWnd();    CFile fis;    TCHAR    tmpPath[_MAX_PATH+1];    GetTempPath(_MAX_PATH,tmpPath);    CString str_FileName;    long l_tempTreeID=m_pMf->m_wndTree.GetCurTreeID();    str_FileName.Format("%s%ld.tmp",tmpPath,l_tempTreeID);    fis.Open(str_FileName, CFile::modeCreate | CFile::modeWrite);//  fis.Open("C:\\a.rtf", CFile::modeRead | CFile::shareExclusive   );    EDITSTREAM es;    es.dwCookie = (DWORD) &fis;    es.pfnCallback = MyStreamOutCallback;     CRichEditCtrl &m_edit = GetRichEditCtrl();    m_edit.StreamOut(SF_RTF, es);    CString str_sql;    // I put the content in the file    fis.Close();    ADO &myado=theApp.m_Ado;    CENBDoc* m_pENBDoc=(CENBDoc*)m_pMf->GetActiveDocument();    if(!myado.OnInitADO(m_pENBDoc->GetPathName()))    {        AfxMessageBox("init ado出错!"+m_pENBDoc->GetPathName());        //return 0;    }    myado.m_pRecordset.CreateInstance(__uuidof(ADODB::Recordset));    str_sql.Format("select d_content,d_contentTxt from T_Tree WHERE D_TREEID=%ld",l_tempTreeID);    myado.m_pRecordset->Open((_variant_t)str_sql,myado.m_pConnection.GetInterfacePtr(),ADODB::adOpenDynamic,ADODB::adLockOptimistic,ADODB::adCmdText);    try    {        //m_pRecordset->AddNew();//添加新行        VARIANT     m_bitdata;        fis.Open(str_FileName,CFile::modeRead);        DWORD m_filelen = fis.GetLength()+1;        char * m_bitbuffer = new char[m_filelen];        fis.ReadHuge(m_bitbuffer,m_filelen);//        fis.Flush();//        fis.Close();        DeleteFile(str_FileName);//删除        m_bitdata.vt= VT_ARRAY|VT_UI1;        SAFEARRAY * m_psafe;        SAFEARRAYBOUND m_band;        m_band.cElements = m_filelen;        m_band.lLbound = 0;        m_psafe = SafeArrayCreate(VT_UI1,1,&m_band);        for(long i=0; i < m_filelen ; i++)        {            SafeArrayPutElement(m_psafe,&i,m_bitbuffer++);        }            m_bitdata.parray = m_psafe;        myado.m_pRecordset->GetFields()->GetItem("d_content")->AppendChunk(&m_bitdata);        CString str_temp;        GetWindowText(str_temp);        myado.m_pRecordset->GetFields()->GetItem("d_contentTxt")->Value=(_bstr_t)str_temp;        myado.m_pRecordset->Update();    }    catch(_com_error e)    {        AfxMessageBox("操作失败:\n"+e.Description());        return 0;    }    CENBDoc *m_pEditDoc = (CENBDoc *)m_pMf->GetActiveDocument();    m_pEditDoc->SetModifiedFlag(0);    m_pMf->m_wndStatusBar.SetPaneText(0,"保存成功!");//    MessageBox("保存成功!",NULL,64);    return 1;}void CENBView::SetRtf(CFile* pInputFile ){    EDITSTREAM es;    es.dwError = 0;    es.pfnCallback = MyStreamInCallback;    es.dwCookie = (DWORD)pInputFile;    CRichEditCtrl &m_edit = GetRichEditCtrl();    m_edit.StreamIn(SF_RTF, es);}
------解决方案--------------------


MK
[解决办法]
看来这个问题快解决了

读书人网 >VC/MFC

热点推荐