#include "StdAfx.h" #include "ADOBaseImpl.h " CADOBaseImpl::CADOBaseImpl(void) { m_bOpen = FALSE; m_pADODatabase = NULL; memset(m_szConnectString,0,1024); InitializeCriticalSection(&m_csAdo); } CADOBaseImpl::~CADOBaseImpl(void) { DeleteCriticalSection(&m_csAdo); } BOOL CADOBaseImpl::OpenCatalog(IN STDatabaseInfo& dbInfo) { if ( m_pADODatabase ) return TRUE; BOOL bRet = FALSE; if ( _ttoi(dbInfo.szDatabaseTCPPort) == 0 ) { _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; "), dbInfo.szDataSource, // IP地址或服务名称; dbInfo.szDatabaseName, dbInfo.szDatabaseAccount, dbInfo.szDatabasePassword); } else { _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; "), dbInfo.szDataSource, // IP地址或服务名称; dbInfo.szDatabaseTCPPort, dbInfo.szDatabaseName, dbInfo.szDatabaseAccount, dbInfo.szDatabasePassword); } m_pADODatabase = new CADODatabase(); if ( m_pADODatabase ) { m_pADODatabase->SetConnectionString(m_szConnectString); if ( m_pADODatabase->Open() ) { bRet = TRUE; } else { if ( m_pADODatabase->IsOpen() ) { m_pADODatabase->Close(); } delete m_pADODatabase; m_pADODatabase = NULL; } } return bRet; } void CADOBaseImpl::CloseCatalog() { if( NULL != m_pADODatabase ) { if( m_pADODatabase->IsOpen() ) { m_pADODatabase->Close(); } if( m_pADODatabase ) delete m_pADODatabase; m_pADODatabase = NULL; } } INT CADOBaseImpl::Execute(IN LPCTSTR lpExcSQL) { INT nRetCode = 0; if ( NULL == m_pADODatabase || NULL == lpExcSQL) return -1; try { EnterCriticalSection(&m_csAdo); if ( m_pADODatabase->IsOpen() ) { nRetCode = m_pADODatabase->Execute(lpExcSQL); LeaveCriticalSection(&m_csAdo); } else { LeaveCriticalSection(&m_csAdo); return -1; } } catch (_com_error* e) { } return nRetCode; } INT CADOBaseImpl::GetTblRecordCount(IN LPCTSTR lpTblName, IN LPCTSTR lpFilters) { if ( lpTblName == NULL) return -1; TCHAR szSQL[1024] = _T(""); if(lpFilters == NULL || _tcscmp(lpFilters,_T("")) == 0) _stprintf_s(szSQL,_T("select count(*) as cot from %s"),lpTblName); else _stprintf_s(szSQL,_T("select count(*) as cot from %s where %s"),lpTblName,lpFilters); DWORD nRecordCount = 0; _variant_t vtFieldValue; BOOL bResult = FALSE; EnterCriticalSection(&m_csAdo); bResult = GetADODateBaseStatus(); if( FALSE == bResult ) { LeaveCriticalSection(&m_csAdo); return -1; } CADORecordset* pRSet = new CADORecordset(m_pADODatabase); if( NULL == pRSet ) { LeaveCriticalSection(&m_csAdo); return -1; } bResult = OpenADORecordSet(pRSet, szSQL); if( false == bResult ) { LeaveCriticalSection(&m_csAdo); DeleteADORecordSet(pRSet); return -1; } if( !pRSet->IsFieldNull(_T("cot")) ) { pRSet->GetFieldValue(_T("cot"), nRecordCount); } else { nRecordCount = -1; } DeleteADORecordSet(pRSet); LeaveCriticalSection(&m_csAdo); return nRecordCount; } BOOL CADOBaseImpl::IsOpen() { return m_pADODatabase == NULL; } BOOL CADOBaseImpl::GetADODateBaseStatus() { if( NULL == m_pADODatabase || !m_pADODatabase->IsOpen()) return FALSE; return TRUE; } BOOL CADOBaseImpl::OpenADORecordSet(IN CADORecordset *pRSet, IN LPCTSTR lpSQL) { if( NULL == pRSet || NULL == lpSQL) return FALSE; INT nRetCode; BOOL bResult = TRUE; try { nRetCode = pRSet->Open(lpSQL, CADORecordset::openQuery); if (nRetCode == 0) { DeleteADORecordSet(pRSet); bResult = FALSE; } } catch (_com_error &e) { DeleteADORecordSet(pRSet); bResult = FALSE; } return bResult; } void CADOBaseImpl::CloseADORecordSet(IN CADORecordset* pRSet) { if( NULL != pRSet ) { if( pRSet->IsOpen() ) { pRSet->Close(); } } } void CADOBaseImpl::DeleteADORecordSet(IN CADORecordset* pRSet) { CloseADORecordSet(pRSet); if( pRSet ) { delete pRSet; pRSet = NULL; } }