关于word文档在SQL中的保存、修改、显示的问题 解决后另加分 我用javascript+vc,现在客户要求能保留原来的word文档。我执行 select * from dali where contains(MyImage,'J老师') 得到的结果是二进制的,这怎么解决? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // VC6 读取sql server 图片// 使用了class CADORecordsetCString 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 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); } 在VC中调用WORD(显示,修改,存盘,运行宏) ZHENG017(原作) (1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"office" (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; } ASSERT(lpDispatch != NULL); return lpDispatch; (5)。在officeView.h添加#include "msword8.h" (6)。修改void COfficeView::OnInsertObject(),源码如下: BeginWaitCursor(); COfficeCntrItem* pItem = NULL; TRY { // Create new item connected to this document. COfficeDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pItem = new COfficeCntrItem(pDoc); ASSERT_VALID(pItem); // Initialize the item from the dialog data. /* if(!dlg.CreateItem(pItem)) AfxThrowMemoryException(); // any exception will do ASSERT_VALID(pItem);*/ CLSID clsid; // 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); ASSERT_VALID(pItem); m_pSelection = pItem; // set selection to last inserted item pDoc->UpdateAllViews(NULL); // As an arbitrary user interface design, this sets the selection // to the lastitem inserted. // TODO: reimplement selection as appropriate for your application m_pSelection = pItem; // set selection to last inserted item pDoc->UpdateAllViews(NULL); } CATCH(CException,e) { if (pItem != NULL) { ASSERT_VALID(pItem); pItem->Delete(); } AfxMessageBox(IDP_FAILED_TO_CREATE); } END_CATCH EndWaitCursor(); (7)重载ID—FILE—SAVE, void COfficeView::OnFileSave() { // TODO: Add your command handler code here TRY { LPDISPATCH lpDisp; lpDisp = m_pSelection->GetIDispatch(); Documents docs; _Application app; _Document mydoc; Documents my; mydoc.AttachDispatch (lpDisp,TRUE); app=mydoc.GetApplication (); /* ("Macro3");*/ 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) { } END_CATCH } bulid,click insert object,and edit ,and save.maybe run macro. 请教2个简单的SQL语句 求一SELECT语句 如何调用存储过程的返回值 如何一次清除多个表中的数据 sql server 2005 里面我在哪个库的哪个表里能看到像2K里面的所有对象信息?? 我想知道如何跟踪触发器的执行情况,多谢了 比較特殊的分組問題! 安装 sql 2008 时出错提示 SQLBrowser”启动请求失败解决办法 请教一个字符串问题 还是有效时间内的记录的查询,请邹捷或其他朋友帮忙 为何select top 10 distinct EmpName from employee order by empid group empid出错? 删除数据库中相同学号的学生(全部)
// 使用了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
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);
}
_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);
}
在VC中调用WORD(显示,修改,存盘,运行宏) ZHENG017(原作) (1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"office"
(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;
} ASSERT(lpDispatch != NULL);
return lpDispatch;
(5)。在officeView.h添加#include "msword8.h"
(6)。修改void COfficeView::OnInsertObject(),源码如下:
BeginWaitCursor();
COfficeCntrItem* pItem = NULL;
TRY
{ // Create new item connected to this document.
COfficeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new COfficeCntrItem(pDoc);
ASSERT_VALID(pItem);
// Initialize the item from the dialog data.
/* if(!dlg.CreateItem(pItem))
AfxThrowMemoryException(); // any exception will do
ASSERT_VALID(pItem);*/
CLSID clsid; //
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);
ASSERT_VALID(pItem);
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
// As an arbitrary user interface design, this sets the selection
// to the lastitem inserted.
// TODO: reimplement selection as appropriate for your application
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
}
CATCH(CException,e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH
EndWaitCursor();
(7)重载ID—FILE—SAVE,
void COfficeView::OnFileSave()
{
// TODO: Add your command handler code here
TRY
{
LPDISPATCH lpDisp; lpDisp = m_pSelection->GetIDispatch();
Documents docs;
_Application app;
_Document mydoc;
Documents my;
mydoc.AttachDispatch (lpDisp,TRUE);
app=mydoc.GetApplication ();
/* ("Macro3");*/
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)
{
}
END_CATCH } bulid,click insert object,and edit ,and save.maybe run macro.