ADOConnection.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:4k
- #include "ADOConnection.h"
- //Initialize or UnInitialize the ADO connection.
- bool CADOConnection::ADO_Initialize()
- {
- return !FAILED(::CoInitialize(NULL));
- }
- void CADOConnection::ADO_UnInitialize()
- {
- ::CoUninitialize();
- }
- // Construction/Destruction
- CADOConnection::CADOConnection()
- {
- if( !CADOConnection::ADO_Initialize() ){
- throw new CADOException( "CADOConnection::CADOConnection", "ADO_Initialize() Failed." );
- }
-
- this->m_bConnected = false;
- this->m_pConnection = NULL;
- this->m_sConnectionString.erase();
- //Create the connection object
- try{
- HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");
- if(FAILED(hr)){
- throw new CADOException( "CADOConnection::CADOConnection", "Create ADO Connection Instance Failed." );
- }
- }
- catch(_com_error e){
- throw new CADOException( "CADOConnection::CADOConnection", e.Description() );
- }
- }
- CADOConnection::~CADOConnection()
- {
- this->Destroy();
- }
- //get the connection smart ptr.
- _ConnectionPtr CADOConnection::getConnectionPtr()
- {
- return this->m_pConnection;
- }
- //Set the connection string and connecting mode.
- bool CADOConnection::Create(LPCSTR lpszConnect,
- ConnectModeEnum cmMode,
- CursorLocationEnum clCursorLocation,
- int nConnectionTimeOut)
- {
- if( !_STREMPTY(lpszConnect) )
- {
- //store the connection string.
- this->m_sConnectionString = lpszConnect;
- try{
- //set the connection properties.
- m_pConnection->CursorLocation = clCursorLocation;
-
- this->m_bConnected = _COMOK(
- m_pConnection->Open(_bstr_t(lpszConnect), "", "", cmMode)
- );
- return this->m_bConnected;
- }
- catch(_com_error e){
- throw new CADOException( "CADOConnection::Create", e.Description() );
- }
- }
- return false;
- }
- //destroy the connection.
- void CADOConnection::Destroy()
- {
- try{
- //close the db connection
- if(this->IsActived()){
- this->m_pConnection->Close();
- this->m_pConnection = NULL;
- this->m_sConnectionString.erase();
- this->m_bConnected = false;
- }
- }
- catch(_com_error e){
- throw new CADOException( "CADOConnection::Destroy", e.Description() );
- }
- }
- //get the connection whether is opened.
- bool CADOConnection::IsActived()
- {
- return this->m_bConnected;
- }
- //get the connection string.
- LPCSTR CADOConnection::GetConnectionString(void)
- {
- return this->m_sConnectionString.data();
- }
- //Transaction processing.
- bool CADOConnection::BeginTransaction()
- {
- try{
- return SUCCEEDED( m_pConnection->BeginTrans() );
- }
- catch(_com_error e){
- throw new CADOException( "CADOConnection::BeginTransaction", e.Description() );
- }
- return false;
- }
- bool CADOConnection::CommitTransaction()
- {
- try{
- return SUCCEEDED( m_pConnection->CommitTrans() );
- }
- catch(_com_error e){
- throw new CADOException( "CADOConnection::CommitTransaction", e.Description() );
- }
- return false;
- }
- bool CADOConnection::RollBackTransaction()
- {
- try{
- return SUCCEEDED( m_pConnection->RollbackTrans() );
- }
- catch(_com_error e){
- throw new CADOException( "CADOConnection::RollBackTransaction", e.Description() );
- }
- return false;
- }
- //Execute some sql statements.
- _RecordsetPtr CADOConnection::Execute(LPCSTR lpszSQLText, CommandTypeEnum ctCmdType)
- {
- _variant_t vRecAffected;
-
- vRecAffected.Clear();
-
- //m_pConnection->CommandTimeout = 0;
- return this->m_pConnection->Execute(
- _bstr_t(lpszSQLText), &vRecAffected, ctCmdType);
- }
- //open schema.
- _RecordsetPtr CADOConnection::OpenSchema(SchemaEnum Schema)
- {
- _RecordsetPtr rs = NULL;
- // Get a list of tables and dump list to console.
- rs = m_pConnection->OpenSchema( Schema );
- return rs;
- }