📄 bbdblib.cpp
字号:
// BBDBlib.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
//#include <afxdllx.h>
#include "BBDBlib.h"
#include <windows.h>
#include <assert.h>
/*
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif*/
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBBAdoConnection::CBBAdoConnection()
{
m_pConn=NULL;
}
_ConnectionPtr* CBBAdoConnection::GetConnection()
{
return m_pConn;
}
/*
CString CBBAdoConnection::GetConnString()
{
return m_szConnString;
}
*/
CBBAdoConnection::~CBBAdoConnection()
{
//关比连接
CloseConnect();
}
BOOL CBBAdoConnection::OpenConnection(char *sConnString,BOOL bReOpen /*=FALSE*/)
{
//不需重新打开
if(!bReOpen)
{
if(m_pConn !=NULL && ((*m_pConn)->State!=adStateClosed))
return TRUE;
}
if(sConnString == NULL)
return FALSE;
strcpy(m_szConnString,sConnString);
try
{
m_pConn =new _ConnectionPtr;
m_pConn->CreateInstance(__uuidof(Connection));
if(m_pConn==NULL)
return FALSE;
HRESULT hr=(*m_pConn)->Open((char*)sConnString,"","",-1);
if(FAILED(hr))
return FALSE;
return TRUE;
}
catch(_com_error)
{
ReportError(ErrFormOpenConnsction);
return FALSE;
}
catch(...)
{
//MessageBox(NULL, "数据库连接未处理的异常" ,"连接失败", MB_OK|MB_ICONINFORMATION);
return FALSE;
}
SetConnTimeOut(5);
}
BOOL CBBAdoConnection::OpenRecordset(const char *sSQL, _RecordsetPtr *rs, char *sConnString)
{
//rs=new _RecordsetPtr;
if(sSQL == NULL)
return FALSE;
try
{
if((m_pConn==NULL)||((*m_pConn)->State==adStateClosed))
{
int n=(*m_pConn)->State;
if(sConnString!=NULL)
{
if(!OpenConnection(sConnString))
return FALSE;
}
//尝试连接数据库
else
{
//
//if(!OpenConnection(m_szConnString))
// return FALSE;
//MessageBox(NULL,"数据库连接已经断开,请重新连接!","连接问题",MB_OK|MB_ICONINFORMATION);
}
return FALSE;
}
//rs=new _RecordsetPtr;
HRESULT hr;
hr=rs->CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
{
return FALSE;//建立实例失败
}
hr=(*rs)->Open(sSQL,m_pConn->GetInterfacePtr(),
adOpenStatic, adLockBatchOptimistic, -1);
if(FAILED(hr))
{
return FALSE;//打开连接失败
}
return TRUE;//成功返回
}
catch(_com_error)
{
//AfxMessageBox(ce->Description());
ReportError(ErrFromOpenRecordset);
return FALSE;
}
catch(...)
{
//MessageBox(NULL, "数据库记录打开失败!", "记录失败", MB_OK|MB_ICONINFORMATION);
return FALSE;
}
return TRUE;
}
void CBBAdoConnection::CloseConnect()
{
try
{
if(m_pConn!=NULL)
{
if((*m_pConn)->State!=adStateClosed)
(*m_pConn)->Close();
delete m_pConn;
m_pConn=NULL;
}
}
catch(_com_error)
{
ReportError(ErrFormCloseConnection);
}
catch(...)
{
// MessageBox(NULL, "关闭数据库连接未知错误!", "提示", MB_OK);
}
}
/*
BOOL CBBAdoConnection::OpenConnection(CString strConnString, BOOL bReOpen)
{
char c[512];
strcpy(c,strConnString.GetBuffer(0));
strConnString.ReleaseBuffer();
return OpenConnection(c,bReOpen);
}
BOOL CBBAdoConnection::OpenRecordset(CString strSQL, _RecordsetPtr *rs, CString sConnString)
{
char c[1024];
strcpy(c,strSQL.GetBuffer(0));
strSQL.ReleaseBuffer();
return OpenRecordset(c,rs,(char*)(LPCTSTR)sConnString);
}
BOOL CBBAdoConnection::ExecuteTrans(CStringArray arrStrSQL) //开始事务处理,不返回任何记录集,参数为事务SQL数组
{
(*m_pConn)->BeginTrans();
try
{
_RecordsetPtr* prsThis;
for(int i=0;i<arrStrSQL.GetSize();i++)
{
prsThis=new _RecordsetPtr;
OpenRecordset(arrStrSQL.ElementAt(i),prsThis);
delete prsThis;
}
prsThis=NULL;
(*m_pConn)->CommitTrans();
return TRUE;
}
catch(_com_error)
{
(*m_pConn)->RollbackTrans();
ReportError(ErrFormTanslation);
return FALSE;
}
}
*/
//执行SQL操作,不返回记录集
int CBBAdoConnection::ExecuteSQL(LPCSTR szSQL)
{
//VARIANT vEffect;
try
{
(*m_pConn)->Execute(szSQL,NULL,adCmdText|adExecuteNoRecords);
return TRUE;
}
catch(_com_error)
{
ReportError(ErrFormTanslation);
return FALSE;
}
//return vEffect.lVal;
}
//返回是否处在连接状态
BOOL CBBAdoConnection::IsConnectClose()
{
return (m_pConn==NULL)||((*m_pConn)->State==adStateClosed);
}
//设置连接超时
int CBBAdoConnection::SetConnTimeOut(long lTimeOut)
{
return (*m_pConn)->put_ConnectionTimeout(lTimeOut);
}
int CBBAdoConnection::SetCommTimeOut(long lTimeOut)
{
return (*m_pConn)->put_CommandTimeout(lTimeOut);
}
//报告错误
void CBBAdoConnection::ReportError(int nERRORfrom)
{
switch(nERRORfrom)
{
case ErrFormOpenConnsction:
#if 1 //def _DEBUG //调试时显示相应的错误信息
try
{
for(long l=0;l<(*m_pConn)->Errors->Count;l++)
{
ErrorPtr pErr;
pErr=(*m_pConn)->Errors->GetItem(l);
/* CString str;
str=(char*)pErr->Description;
MessageBox(NULL,str,"连接失败",MB_OK|MB_ICONINFORMATION);*/
}
}
catch(...)
{
///MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息!","错误",MB_ICONINFORMATION);
}
#else
// MessageBox(NULL,"连接数据失败,请检查网络和数据库设置是否正确","连接失败",MB_OK|MB_ICONINFORMATION);
#endif
break;
case ErrFromOpenRecordset:
#if 1 //def _DEBUG
try
{
for(long i=0;i<(*m_pConn)->Errors->Count;i++)
{
ErrorPtr pErr;
pErr=(*m_pConn)->Errors->GetItem(i);
// AfxMessageBox(pErr->Description);
}
}
catch(...)
{
//MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息!","错误",MB_ICONINFORMATION);
}
#else
//MessageBox(NULL,"打开数据库失败,请检查网络,并尝试重新连接数据库!","记录失败",MB_OK|MB_ICONINFORMATION);
#endif
break;
case ErrFormCloseConnection:
#if 1 //def _DEBUG //调试时显示相应的错误信息
try
{
for(long l=0;l<(*m_pConn)->Errors->Count;l++)
{
ErrorPtr pErr;
pErr=(*m_pConn)->Errors->GetItem(l);
/* CString str;
str=(char*)pErr->Description;
MessageBox(NULL,str,"连接失败",MB_OK|MB_ICONINFORMATION);*/
}
}
catch(...)
{
// MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息!","错误",MB_ICONINFORMATION);
}
#else
;//MessageBox(NULL,"关闭数据库连接异常","关闭异常",MB_OK|MB_ICONINFORMATION);
#endif
break;
case ErrFormTanslation:
#if 1 ////def _DEBUG
try
{
for(long i=0;i<(*m_pConn)->Errors->Count;i++)
{
ErrorPtr pErr;
pErr=(*m_pConn)->Errors->GetItem(i);
//AfxMessageBox(pErr->Description);
}
}
catch(...)
{
//MessageBox(NULL,"数据库连接未知错误,无法捕捉具体错误信息!","错误",MB_ICONINFORMATION);
}
#else
//MessageBox(NULL,"数据库执行任务失败,请检查数据库。","任务失败",MB_OK|MB_ICONINFORMATION);
#endif
break;
default:
break;
}
}
///////////////CRecordsetValus
CBBRstValues::CBBRstValues()
{
;
}
CBBRstValues::CBBRstValues(_ConnectionPtr* pConn,_RecordsetPtr* pRs)
{
assert(pConn);
assert(pRs);
m_prsThis=pRs;
m_pConn=pConn;
}
CBBRstValues::CBBRstValues(CBBAdoConnection* pBBadoConn,_RecordsetPtr* pRs)
{
CBBRstValues(pBBadoConn->GetConnection(),pRs);
}
CBBRstValues::~CBBRstValues()
{
;
}
void CBBRstValues::InitConnectAndRst(_ConnectionPtr *pConn, _RecordsetPtr *pRs)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -