123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #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;
- }
- }
|