www.pudn.com > RadiusSrv.rar > DBManager.cpp, change:2003-10-24,size:3184b


#include "DBManager.h" 
#include "..\common\log.h" 
 
#pragma warning(disable:4761) //(LPCSTR)bstrDescription 
 
DBManager::DBManager() 
{ 
	m_pConnection=NULL; 
	m_strPrepareStmt.erase(); 
} 
 
DBManager::~DBManager() 
{ 
	Close(); 
} 
 
/*******************************************************************\ 
|函数名:	bool DBInit(_DB_INFO &dbMaster) 
|参数:      _DB_INFO &dbMaster   数据库连接信息   
|返回值:    初始化是否成功  
|功能:		初始化连接数据库 
|创建时间: 2003-9-9 
\*******************************************************************/ 
bool DBManager::Open(const char *strConnection, const char *strUserID, const char *strPassword) 
{ 
	if(strlen(strConnection)<10) 
		return false; 
	::CoInitialize(NULL); 
 
	HRESULT hr; 
 
	try 
	{ 
		hr=m_pConnection.CreateInstance("ADODB.Connection"); 
		if(SUCCEEDED(hr)) 
		{ 
			m_pConnection->ConnectionTimeout=10; 
		    hr=m_pConnection->Open(strConnection, strUserID, strPassword, adModeUnknown); 
			m_pCommand.CreateInstance("ADODB.Command"); 
			m_pCommand->ActiveConnection=m_pConnection;		 
		} 
	 
		m_pRecordset.CreateInstance("ADODB.Recordset"); 
		m_pParameter.CreateInstance("ADODB.Parameter"); 
	} 
	catch(_com_error e) 
	{ 
		_bstr_t bstrSource(e.Source()); 
		_bstr_t bstrDescription(e.Description()); 
 
		stringstream stmtemp; 
 
		stmtemp<<"错误源是:"<<(LPCSTR)bstrSource<<"错误描述是:"<<(LPCSTR)bstrDescription; 
		LOG(3) (stmtemp.str().c_str()); 
		return false; 
	} 
 
	return true; 
} 
 
void DBManager::Close() 
{ 
	if(m_pConnection) 
		m_pConnection->Close(); 
	m_pConnection=NULL; 
} 
/*******************************************************************\ 
|函数名:	void prepareStatement(const string &strPrepareStmt)    
|参数:      const string &strPrepareStmt      查询或者执行语句 
|返回值:    无 
|功能:		设置查询字符串 
|创建时间: 2003-9-9 
\*******************************************************************/ 
void DBManager::prepareStatement(const string &strPrepareStmt) 
{ 
	m_strPrepareStmt=strPrepareStmt; 
} 
 
/*******************************************************************\ 
|函数名:	executeQuery(void) 
|参数:      无 
|返回值:    结果数据集 
|功能:		执行查询并返回数据集 
|创建时间: 2003-9-9 
\*******************************************************************/ 
_RecordsetPtr DBManager::executeQuery(void) 
{ 
	try 
	{ 
		return m_pConnection->Execute((LPSTR)_bstr_t(m_strPrepareStmt.c_str()),&m_RecordsAffected,adCmdText); 
	} 
	catch(_com_error &e) 
	{ 
		_bstr_t bstrSource(e.Source()); 
		_bstr_t bstrDescription(e.Description()); 
 
		stringstream stmtemp; 
 
		stmtemp<<"错误源是:"<<(LPCSTR)bstrSource<<"错误描述是:"<<(LPCSTR)bstrDescription; 
		LOG(3) (stmtemp.str().c_str()); 
		return NULL; 
	} 
} 
 
_RecordsetPtr DBManager::executeQuery(string &strPreStmt) 
{ 
	try 
	{ 
		return m_pConnection->Execute((LPSTR)_bstr_t(strPreStmt.c_str()),&m_RecordsAffected,adCmdText); 
	} 
	catch(_com_error &e) 
	{ 
		_bstr_t bstrSource(e.Source()); 
		_bstr_t bstrDescription(e.Description()); 
 
		stringstream stmtemp; 
 
		stmtemp<<"错误源是:"<<(LPCSTR)bstrSource<<"错误描述是:"<<(LPCSTR)bstrDescription; 
		LOG(3) (stmtemp.str().c_str()); 
		return NULL; 
	} 
}