VC6.0利用ado元件 向oracle資料庫插入圖片
oracle 資料庫中用Bolb儲存圖片
一、資料庫表設定
1、建立表
create table epoliceadmin.ep_image(image_id number(10) not null primary key, image blob);
2、建立序列
create sequence emp_sequence
increment by 1-- 每次加幾個
start with 1--從幾開始
nomaxvalue--不設定最大值
nocycle--不迴圈
nocache--無緩衝區
3、建立觸發器
create trigger tb_tri before insert on epoliceadmin.ep_image for each row
begin
select emp_sequence.nextval into :new.image_id from dual;
end;
VC++程式碼
1、在stdafx.h檔案中加入
#import "c:\program files\common files\system\ado\msado15.dll"no_namespacerename("EOF","adoEOF")
2、在類標頭檔案中加入
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
int m_filelen;
char* m_pfilebuf;
int GetMaxIDFromBlob(void);
3、初始化資料庫連線
CoInitialize(NULL);
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
{
AfxMessageBox("Create ADO Connection Failed!");
}
try
{
hr = m_pConnection->Open("Provider=OraOLEDB.Oracle.1;Data Source=epolice", "epoliceadmin", "epoliceadmin", adModeUnknown);
if (FAILED(hr))
{
AfxMessageBox("Cannot connect to database!");
}
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
4、完成GetMaxIDFromBlob(void)函式
int 類名::GetMaxIDFromBlob()
{
int MaxId;
CString strSQL;
strSQL.Format("select count(*) as num, Max(image_id) as maxid from epoliceadmin.ep_image");
BSTR bstrSQL = strSQL.AllocSysString();
HRESULT hr = m_pRecordset.CreateInstance("ADODB.Recordset");
if (FAILED(hr))
{
AfxMessageBox("Create ADO Recordset Failed!");
}
try
{
hr = m_pRecordset->Open(bstrSQL, (IDispatch*)m_pConnection, adOpenDynamic, adLockOptimistic, adCmdText);
if (FAILED(hr))
{
AfxMessageBox("Open ADO Recordset Failed");
}
}
catch (_com_error e)
{
AfxMessageBox("get maxid error");
return 0;
}
_variant_t var = m_pRecordset->GetCollect("num");
var.ChangeType(VT_I4, NULL);
int num = V_I4(&var);
if (num != 0)
{
_variant_t var1 = m_pRecordset->GetCollect("maxid");
var1.ChangeType(VT_I4, NULL);
MaxId = V_I4(&var1);
}
else
{
MaxId = 0;
}
m_pRecordset->Close();
return MaxId;
}
5、插入圖片到資料庫
HRESULT hr = m_pRecordset.CreateInstance("ADODB.Recordset");
if (FAILED(hr))
{
AfxMessageBox("Create ADO Recordset Failed!");
}
int maxid;
maxid = GetMaxIDFromBlob();
CString strSQL;
strSQL.Format("select * from epoliceadmin.ep_image where image_id= %d", maxid);
BSTR bstrSQL = strSQL.AllocSysString();
try
{
m_pConnection->CursorLocation = adUseClient;//使用客戶端遊標
m_pRecordset->Open(bstrSQL, (IDispatch*)m_pConnection, adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
CString strComError;
strComError.Format("錯誤編號: %08lx\n錯誤資訊: %s\n錯誤源: %s\n錯誤描述: %s",
e.Error(), // 錯誤編號
e.ErrorMessage(), // 錯誤資訊
(LPCSTR) e.Source(), // 錯誤源
(LPCSTR) e.Description()); // 錯誤描述
AfxMessageBox(strComError);
}
CString imagename;
CString imagepath = _T("c:\\picture\\picture_1.jpg");
int index = imagepath.ReverseFind('\\');
imagename = imagepath.Mid(index+1);
CFile file;
if (!file.Open(imagepath, CFile::modeRead))
{
AfxMessageBox("Open file failed");
exit(0);
}
m_filelen = file.GetLength();
m_pfilebuf = new char[m_filelen + 1];
memset(m_pfilebuf, 0, m_filelen + 1);
if (NULL == m_pfilebuf)
{
printf("malloc error!");
}
if (file.Read(m_pfilebuf, m_filelen) != m_filelen)
{
printf("read file failed");
}
try
{
if (!m_pRecordset->Supports(adAddNew))
{
printf("AddNew failed");
}
m_pRecordset->Requery(adOptionUnspecified);
m_pRecordset->AddNew();//為記錄集新增新的一行,更新時就會把這條新紀錄放到資料庫中
}
catch (_com_error e)
{
CString strComError;
strComError.Format("錯誤編號: %08lx\n錯誤資訊: %s\n錯誤源: %s\n錯誤描述: %s",
e.Error(), // 錯誤編號
e.ErrorMessage(), // 錯誤資訊
(LPCSTR) e.Source(), // 錯誤源
(LPCSTR) e.Description()); // 錯誤描述
AfxMessageBox(strComError);
}
try
{
int NewId = maxid + 1;
m_pRecordset->PutCollect("image_id", _variant_t((long)NewId));
//////////////////////////////blob
char *pBuf = m_pfilebuf;
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
if(pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = m_filelen;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); ///建立SAFEARRAY物件
for (long i = 0; i < (long)m_filelen; i++)
SafeArrayPutElement(psa, &i, pBuf++); ///將pBuf指向的二進位制資料儲存到SAFEARRAY物件psa中
varBLOB.vt = VT_ARRAY | VT_UI1; ///將varBLOB的型別設定為BYTE型別的陣列
varBLOB.parray = psa; ///為varBLOB變數賦值
m_pRecordset->GetFields()->GetItem("image")->AppendChunk(varBLOB);///加入BLOB型別的資料
}
}
catch(_com_error e)
{
CString strComError;
strComError.Format("錯誤編號: %08lx\n錯誤資訊: %s\n錯誤源: %s\n錯誤描述: %s",
e.Error(), // 錯誤編號
e.ErrorMessage(), // 錯誤資訊
(LPCSTR) e.Source(), // 錯誤源
(LPCSTR) e.Description()); // 錯誤描述
AfxMessageBox(strComError);
}
m_pRecordset->Update();
m_pRecordset->Close();
m_pRecordset = NULL;
delete m_pfilebuf;
m_pfilebuf = NULL;
最後釋放COM元件
CoUninitialize();