如何用ado将二进制图片数据存入mysql数据库
2个回答
展开全部
用ADO方式连的MYSQL, 已经用AppendChunk图片以二进制插入数据库中,下面是读取的不分,写入相反的方式
void DatabaseFile::GetImage()
{
_RecordsetPtr pRs = NULL;
pRs.CreateInstance(__uuidof(Recordset));
_variant_t varChunk;
_variant_t varBLOB;
try
{
CString sql = "select image from visdata where DetectTime = '2008-06-19 11:23:44';";
//Open a Recordset
HRESULT hr = pRs->Open(_variant_t("select * from visdata;"),_variant_t((IDispatch *) m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdText);
//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);//分配空间
hr = SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
if (FAILED(hr))
return;
//Build a File
char tmpPath[_MAX_PATH+1];
GetCurrentDirectory( MAX_PATH,tmpPath);
CString strFileName = "\\temp2.bmp";//输出图片的文件名
strFileName = tmpPath+strFileName;
CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary );
/*要判断文件是否创建成功*/
LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)(pBuf));
outFile.WriteHuge(buffer,lDataLength);
GlobalUnlock((HGLOBAL)pBuf);
outFile.Close();
SafeArrayUnaccessData (varBLOB.parray);
}
pRs->Close();
}
void DatabaseFile::GetImage()
{
_RecordsetPtr pRs = NULL;
pRs.CreateInstance(__uuidof(Recordset));
_variant_t varChunk;
_variant_t varBLOB;
try
{
CString sql = "select image from visdata where DetectTime = '2008-06-19 11:23:44';";
//Open a Recordset
HRESULT hr = pRs->Open(_variant_t("select * from visdata;"),_variant_t((IDispatch *) m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdText);
//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);//分配空间
hr = SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
if (FAILED(hr))
return;
//Build a File
char tmpPath[_MAX_PATH+1];
GetCurrentDirectory( MAX_PATH,tmpPath);
CString strFileName = "\\temp2.bmp";//输出图片的文件名
strFileName = tmpPath+strFileName;
CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary );
/*要判断文件是否创建成功*/
LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)(pBuf));
outFile.WriteHuge(buffer,lDataLength);
GlobalUnlock((HGLOBAL)pBuf);
outFile.Close();
SafeArrayUnaccessData (varBLOB.parray);
}
pRs->Close();
}
2017-05-25
展开全部
用ADO方式连的MYSQL, 已经用AppendChunk图片以二进制插入数据库中,下面是读取的不分,写入相反的方式
void DatabaseFile::GetImage()
{
_RecordsetPtr pRs = NULL;
pRs.CreateInstance(__uuidof(Recordset));
_variant_t varChunk;
_variant_t varBLOB;
try
{
CString sql = "select image from visdata where DetectTime = '2008-06-19 11:23:44';";
//Open a Recordset
HRESULT hr = pRs->Open(_variant_t("select * from visdata;"),_variant_t((IDispatch *) m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdText);
//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);//分配空间
hr = SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
if (FAILED(hr))
return;
//Build a File
char tmpPath[_MAX_PATH+1];
GetCurrentDirectory( MAX_PATH,tmpPath);
CString strFileName = "\\temp2.bmp";//输出图片的文件名
strFileName = tmpPath+strFileName;
CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary );
/*要判断文件是否创建成功*/
LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)(pBuf));
outFile.WriteHuge(buffer,lDataLength);
GlobalUnlock((HGLOBAL)pBuf);
outFile.Close();
SafeArrayUnaccessData (varBLOB.parray);
}
pRs->Close();
}
void DatabaseFile::GetImage()
{
_RecordsetPtr pRs = NULL;
pRs.CreateInstance(__uuidof(Recordset));
_variant_t varChunk;
_variant_t varBLOB;
try
{
CString sql = "select image from visdata where DetectTime = '2008-06-19 11:23:44';";
//Open a Recordset
HRESULT hr = pRs->Open(_variant_t("select * from visdata;"),_variant_t((IDispatch *) m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdText);
//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);//分配空间
hr = SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
if (FAILED(hr))
return;
//Build a File
char tmpPath[_MAX_PATH+1];
GetCurrentDirectory( MAX_PATH,tmpPath);
CString strFileName = "\\temp2.bmp";//输出图片的文件名
strFileName = tmpPath+strFileName;
CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary );
/*要判断文件是否创建成功*/
LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)(pBuf));
outFile.WriteHuge(buffer,lDataLength);
GlobalUnlock((HGLOBAL)pBuf);
outFile.Close();
SafeArrayUnaccessData (varBLOB.parray);
}
pRs->Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询