📄 ado_image_write_read.cpp
字号:
// ADO_Image_write_read.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ADO_Image_write_read.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
bool WriteImage()
{
CFile imagefile;
if(0 == imagefile.Open("C:\\Documents and Settings\\Administrator\\My Documents\\my\\My Pictures\\A8180256MG.jpg",CFile::modeRead))
return false;
_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 false; //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");
CString str("Provider=SQLOLEDB.1; Persist Security Info=False; Data Source=");
str += "192.168.1.149";
try
{
//Open a connection
pConnection.CreateInstance(__uuidof(Connection));
// hr = pConnection->Open(strCnn,"","",NULL); //Connect a DataBase
//
// hr = pConnection->Open((LPCSTR)str,(LPCSTR)"sa",
// (LPCSTR)"sa",
// adConnectUnspecified);
// pConnection->CursorLocation = adUseClient;
pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///连接数据库
//
// 数据库sperm是否存在
// _bstr_t sqlDBEXIST("select 1 from master..sysdatabases where name = 'sperm'");
// _RecordsetPtr ptr("adodb.recordset");
// ptr->Open(sqlDBEXIST,pConnection.GetInterfacePtr(),
// adOpenStatic,adLockOptimistic,adCmdUnknown);
//
// CString strSelect;
// strSelect.Format("select * from sperm..image");
// _RecordsetPtr rs("adodb.recordset");
// rs->PutCursorLocation(adUseClient);
// rs->Open((LPCSTR)strSelect,pConnection.GetInterfacePtr(),adOpenStatic,
// adLockOptimistic,adCmdText);
_RecordsetPtr rs;
rs.CreateInstance("ADODB.Recordset");
rs->Open("SELECT * FROM jlab ORDER BY id",
_variant_t((IDispatch*)pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);
//
rs->AddNew();
rs->Fields->GetItem("j")->AppendChunk(varBLOB);
rs->Update();
rs->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);
}
return true;
}
bool ReadImage()
{
_ConnectionPtr pConnection = NULL;
_variant_t varChunk;
HRESULT hr;
_bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER");
CString str("Provider=SQLOLEDB.1; Persist Security Info=False; Data Source=");
str += "192.168.1.149";
try
{
//Open a connection
pConnection.CreateInstance("ADODB.Connection");
//hr = pConnection->Open(strCnn,"","",NULL);
//
// hr = pConnection->Open((LPCSTR)str,(LPCSTR)"sa",
// (LPCSTR)"sa",
// adConnectUnspecified);
// pConnection->CursorLocation = adUseClient;
pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///连接数据库
//
// 数据库sperm是否存在
// _bstr_t sqlDBEXIST("select 1 from master..sysdatabases where name = 'sperm'");
// _RecordsetPtr ptr("adodb.recordset");
// ptr->Open(sqlDBEXIST,pConnection.GetInterfacePtr(),
// adOpenStatic,adLockOptimistic,adCmdUnknown);
//
// CString strSelect;
// strSelect.Format("select * from sperm..image");
// _RecordsetPtr rs("adodb.recordset");
// rs->PutCursorLocation(adUseClient);
// rs->Open((LPCSTR)strSelect,pConnection.GetInterfacePtr(),adOpenStatic,
// adLockOptimistic,adCmdText);
//read data
_RecordsetPtr rs;
rs.CreateInstance("ADODB.Recordset");
rs->Open("SELECT * FROM jlab ORDER BY id",
_variant_t((IDispatch*)pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);
rs->MoveLast();
long lDataLength = rs->Fields->GetItem("j")->ActualSize;
_variant_t varBLOB;
varBLOB = rs->GetFields()->GetItem("j")->GetChunk(lDataLength);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
LPBYTE pBuffer = new BYTE[lDataLength+1];
char *pBuf = NULL;
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(pBuffer,pBuf,lDataLength); //复制数据到缓冲区m_pBMPBuffer
SafeArrayUnaccessData (varBLOB.parray);
//Build a File in Windows Temp Directory
CString strFileName = "C:\\Documents and Settings\\Administrator\\桌面\\ado_jpg\\test.jpg";
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);
}
rs->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);
}
return true;
// try
// {
// _ConnectionPtr pConnection;
// pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
//
// pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);///连接数据库
//
// _RecordsetPtr pRecordset;
// pRecordset.CreateInstance("ADODB.Recordset");
// pRecordset->Open("SELECT * FROM jlab ORDER BY id",
// _variant_t((IDispatch*)pConnection,true),
// adOpenStatic,adLockOptimistic,adCmdText);
//
// pRecordset->MoveFirst();
// long nSize = pRecordset->GetFields()->GetItem("j")->ActualSize;
// if(nSize > 0)
// {
// _variant_t varBLOB;
// varBLOB = pRecordset->GetFields()->GetItem("j")->GetChunk(nSize);
// if(varBLOB.vt == (VT_ARRAY | VT_UI1))
// {
// if(BYTE *pBuffer = new BYTE [nSize+1]) ///重新申请必要的存储空间
// {
// char *pBuf = NULL;
// SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
// memcpy(pBuffer,pBuf,nSize); ///复制数据到缓冲区m_pBMPBuffer
// SafeArrayUnaccessData (varBLOB.parray);
// // int nSize = lDataSize;
//
// CString strFileName = "C:\\Documents and Settings\\Administrator\\桌面\\ado_jpg\\test.jpg";
//
// CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite);
//
// LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)pBuf);
// outFile.WriteHuge(buffer,nSize);
// GlobalUnlock((HGLOBAL)pBuf);
// outFile.Close();
//
// delete [] pBuffer;
// pBuf=0;
//
// }
// }
// }
// }
// catch (_com_error e) {
// }
// return true;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
::CoInitialize(NULL);
WriteImage();
ReadImage();
}
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -