如何在vc中对二进制文件进行数据库的查询及显示查询结果的操作??? 各位高手: 请问如何通过vc编程来建一个数据库(而不使用现有的商业软件)实现直接对二进制文件的查询和显示的操作?我要晕掉了,请高手们出手相救! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我在一篇文章上看到的,我现在是要做存储的部分,不知道下面的程序对你有用不关于用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); } 这段代码VC读出数据库中内容的部分有个错误SafeArrayAccessData(vOleObj.parray,(void **)pBuf);应该为SafeArrayAccessData(vOleObj.parray,(void **)&pBuf);因为:(void **)&pBuf 一个指向空指针类型的指针变量,也就是指向指针的指针。前面有定义BYTE *pBuf 。由于是一个指向指针的变量,自己应该是个指向地址的变量,这个地址是一个指针变量的地址,而不是那个指针所指向的地址。所以如果写成(void **)pBuf就是错的,因为把那个指针指向的地址赋给了指向指针的变量。应该用&取地址符,得到那个指针的地址赋值。 TCP传输文件的问题?疑惑比较多 怎么在ActiveX控件上放置一个图片? 关于采用TCP而不采用IOCP的负载问题,盼复! 高手们请问??? ■■IE的默认页被改了,■怎么办? 本机通过局域网另外一台电脑上网,如何才能获取外网IP?? 怎样,改变windows窗口尺寸 谁删除过 win me C:\_RESTORE 下的隐藏文件? 这个文件夹太大了, 占了1.2G, 我C盘都给它吃掉了。 字符存储问题 怎么做象office里那样的帮助小人动画 怎么设计这样一个variant数组,在函数中返回,在另一个函数中解析? 各位大侠,内存映射文件问题>>
关于用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);
} 这段代码VC读出数据库中内容的部分有个错误
SafeArrayAccessData(vOleObj.parray,(void **)pBuf);应该为SafeArrayAccessData(vOleObj.parray,(void **)&pBuf);
因为:
(void **)&pBuf 一个指向空指针类型的指针变量,也就是指向指针的指针。前面有定义BYTE *pBuf 。
由于是一个指向指针的变量,自己应该是个指向地址的变量,这个地址是一个指针变量的地址,而不
是那个指针所指向的地址。所以如果写成(void **)pBuf就是错的,因为把那个指针指向的地址赋给了
指向指针的变量。应该用&取地址符,得到那个指针的地址赋值。