// ImageForDB.cpp: implementation of the CImageForDB class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ImageForDB.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CImageForDB::CImageForDB()
{
if(!AfxOleInit())//这就是初始化COM库
{
AfxMessageBox("OLE初始化出错!");
return;
}
if (FAILED(m_pConnection.CreateInstance("ADODB.Connection")))//创建连接对象实例
{
AfxMessageBox("无法创建连接数据库实例m_pConnection");
return;
}
m_pRecordset.CreateInstance("ADODB.Recordset");
srand((unsigned)time(NULL));
}
CImageForDB::~CImageForDB()
{
}
/////////////////////////////////////////////////////////
//SetDB(CString strIP,CString strDB,CString strUser,CString strPW)
//设置数据库(SQL Seryer)
// m_strIP 数据库IP地址
// m_strDB 数据库名称
// m_strUser 用户名
// m_strPW 密码
/////////////////////////////////////////////////////////
void CImageForDB::SetDB(CString strIP,CString strDB,CString strUser,CString strPW)
{
m_strIP=strIP;
m_strDB=strDB;
m_strUser=strUser;
m_strPW=strPW;
m_strConnect.Format("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=%s;Initial Catalog=%s;Data Source=%s",
m_strUser,m_strDB,m_strIP);
}
/////////////////////////////////////////////////////////
//SetDB(CString strIP,CString strDB,CString strUser,CString strPW)
//设置数据库(Access)
// m_strDB 数据库名称
/////////////////////////////////////////////////////////
void CImageForDB::SetDB(CString strDB)
{
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos = sPath.ReverseFind ('\\');
sPath = sPath.Left (nPos);
m_strDB=strDB;
m_strUser="";
m_strPW="";
m_strIP="";
m_strConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s\\%s.mdb",sPath,m_strDB);
_CatalogPtr m_pCatalog;//(ADO)用于创建数据库
try//创建ACCESS数据库
{
m_pCatalog.CreateInstance(__uuidof(Catalog));
m_pCatalog->Create((_bstr_t)m_strConnect);
}
catch(_com_error e)
{}
}
/////////////////////////////////////////////////////////
//SetTable(CString strTable, CString strID, CString strName, CString strImage)
//设置数据表
// strTable 数据表名
// strID 数据库所保存记录的唯一标识符,用于查找、提取对应图片
// strName 用于保存图片名称的数据库列的字段名
// strImage 用于保存图片的数据库列的字段名,类型为Image(SQL Server)或OLE(Access)
/////////////////////////////////////////////////////////
void CImageForDB::SetTable(CString strTable, CString strID, CString strName, CString strImage)
{
m_strTable=strTable;
m_strID=strID;
m_strName=strName;
m_strImage=strImage;
if (m_strIP.IsEmpty())
{
if (m_pConnection->State)
{
m_pConnection->Close();
}
try
{
m_pConnection->ConnectionTimeout = 5;
m_pConnection->Open((_bstr_t)m_strConnect,(_bstr_t)m_strUser,(_bstr_t)m_strPW,adModeUnknown);//打开数据库
}
catch (_com_error e)//捕捉错误
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return;
}
_variant_t RecordsAffected;//(ADO)操作后所影响的行数
try//尝试创建表格
{
CString strSql;
strSql.Format("CREATE TABLE %s ([%s] AUTOINCREMENT(1,1),[%s] TEXT,[%s] IMAGE)",m_strTable,strID,strName,strImage);
m_pConnection->Execute((_bstr_t)strSql,&RecordsAffected,adCmdText);
}
catch(_com_error e)///捕捉异常
{}
if (m_pConnection->State)
{
m_pConnection->Close();
}
}
}
/////////////////////////////////////////////////////////
//Image2DB(CWnd* pWnd)
//保存图片到数据库
// pWnd 调用本函数的对话框的指针
/////////////////////////////////////////////////////////
BOOL CImageForDB::Image2DB(CWnd* pWnd)
{
if (!CheckDBTable())
{
return FALSE;
}
CString strSql;
strSql.Format("select * from %s",m_strTable);
_variant_t varBLOB;
_StreamPtr p_Stream;
p_Stream.CreateInstance("ADODB.Stream");
p_Stream->put_Type(adTypeBinary);//类型为二进制
CString strFilePathName;
CFileDialog dlg(TRUE,//TRUE为OPEN对话框, FALSE为SAVE AS对话框
NULL,NULL,0,"图像文件 (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif||",pWnd);
if (dlg.DoModal()==IDOK)
{
strFilePathName = dlg.GetPathName();//取得文件名
int nPos = strFilePathName.ReverseFind ('\\');
strFilePathName.Delete(0,nPos+1);
_variant_t varOptinal(DISP_E_PARAMNOTFOUND,VT_ERROR);
p_Stream->Open(varOptinal,adModeUnknown,adOpenStreamUnspecified,_bstr_t(),_bstr_t());//生成二进制
try
{
p_Stream->LoadFromFile(_bstr_t(strFilePathName));
}
catch (_com_error e)
{
m_pConnection->Close();
AfxMessageBox("文件打开失败");
return FALSE;
}
varBLOB =p_Stream->Read(adReadAll);//输入数据至variant变量
if (m_pConnection->State)
{
m_pConnection->Close();
}
try
{
m_pConnection->ConnectionTimeout = 5;
m_pConnection->Open((_bstr_t)m_strConnect,(_bstr_t)m_strUser,(_bstr_t)m_strPW,adModeUnknown);//打开数据库
}
catch (_com_error e)//捕捉错误
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
//AfxMessageBox("连接成功");
//保存到数据库
m_pRecordset->Open(_bstr_t(strSql),_variant_t((IDispatch*)m_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);//直接用Recordset对象进行查询取得记录集
m_pRecordset->AddNew();//增加一条记录
m_pRecordset->GetFields()->GetItem(( _variant_t)m_strImage)->Value=varBLOB;//保存到数据集对象
m_pRecordset->PutCollect(( _variant_t)m_strName,( _variant_t)strFilePathName);
m_pRecordset->Update();
if (m_pRecordset->State)
{//关闭记录集
m_pRecordset->Close();
}
if (m_pConnection->State)
{//断开数据库连接
m_pConnection->Close();
}
varBLOB.Clear();
p_Stream->Close();
return TRUE;
}
return FALSE;
}
/////////////////////////////////////////////////////////
//DB2Image(CWnd *pWnd,CString strID)
//导出数据库图片
// pWnd 调用本函数的对话框的指针
// strID 将导出的图片的唯一标示符内容
/////////////////////////////////////////////////////////
BOOL CImageForDB::DB2Image(CWnd *pWnd,CString strID)
{
if (!CheckDBTable())
{
return FALSE;
}
CString strSql;
if (strID.IsEmpty())
{
AfxMessageBox("请选择图像");
return FALSE;
}
strSql.Format("select * from %s where ID=%s",m_strTable,strID);
_variant_t varBLOB;
_StreamPtr p_Stream;
p_Stream.CreateInstance("ADODB.Stream");
p_Stream->put_Type(adTypeBinary);//类型为二进制
CString strFilePathName;
CFileDialog dlg(FALSE,//TRUE为OPEN对话框, FALSE为SAVE AS对话框
".jpg",RaneName(10),0,"jpg Files(*.jpg)|*.jpg|bmp Files(*.bmp)|*.bmp|gif Files(*.gif)|*.gif|all Files(*.*)|*.*||",pWnd);
if (dlg.DoModal()==IDOK)
{
strFilePathName = dlg.GetPathName();//取得文件名
_variant_t varOptinal(DISP_E_PARAMNOTFOUND,VT_ERROR);
p_Stream->Open(varOptinal,adModeUnknown,adOpenStreamUnspecified,_bstr_t(),_bstr_t());//生成二进制流
if (m_pConnection->State)
{
m_pConnection->Close();
}
try
{
m_pConnection->ConnectionTimeout = 5;
m_pConnection->Open(_bstr_t(m_strConnect),(_bstr_t)m_strUser,(_bstr_t)m_strPW,adModeUnknown); //打开数据库
}
catch (_com_error e)//捕捉错误
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
//AfxMessageBox("连接成功");
m_pRecordset->Open(_bstr_t(strSql),_variant_t((IDispatch*)m_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdT
评论2
最新资源