WEB开发网
开发学院软件开发VC VC/VB 进行图像数据存储数据库心得 阅读

VC/VB 进行图像数据存储数据库心得

 2007-03-22 21:31:50 来源:WEB开发网   
核心提示: _RecordsetPtr pRs = NULL;_ConnectionPtr pConnection = NULL;_variant_t varChunk;HRESULT hr;VARIANT varBLOB;_bstr_t strCnn("Provider=SQLOLEDB.
_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 Description : %s ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);
}

三、VB把文件存入数据库IMAGE字段

Tags:VC VB 进行

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接