123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- #include "StdAfx.h"
- #include "Ado2Impl.h"
- Ado2Impl::Ado2Impl(void)
- {
- m_bOpen = FALSE;
- m_pADODatabase = NULL;
- memset(m_szConnectString,0,1024);
- InitializeCriticalSection(&m_csAdo);
- }
- Ado2Impl::~Ado2Impl(void)
- {
- CloseCatalog();
- DeleteCriticalSection(&m_csAdo);
- }
- BOOL Ado2Impl::OpenCatalog(IN Catalogkernel::STDatabaseInfo& dbInfo)
- {
- if ( m_pADODatabase ) return TRUE;
- BOOL bRet = FALSE;
- if ( !_tcsicmp(dbInfo.szDatabaseTCPPort, _T("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 Ado2Impl::CloseCatalog()
- {
- if( NULL != m_pADODatabase )
- {
- if( m_pADODatabase->IsOpen() )
- {
- m_pADODatabase->Close();
- }
- if( m_pADODatabase )
- delete m_pADODatabase;
- m_pADODatabase = NULL;
- }
- }
- INT Ado2Impl::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 Ado2Impl::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 Ado2Impl::IsOpen()
- {
- return m_pADODatabase == NULL;
- }
- BOOL Ado2Impl::GetADODateBaseStatus()
- {
- if( NULL == m_pADODatabase || !m_pADODatabase->IsOpen())
- return FALSE;
- return TRUE;
- }
- BOOL Ado2Impl::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 Ado2Impl::CloseADORecordSet(IN CADORecordset* pRSet)
- {
- if( NULL != pRSet )
- {
- if( pRSet->IsOpen() )
- {
- pRSet->Close();
- }
- }
- }
- void Ado2Impl::DeleteADORecordSet(IN CADORecordset* pRSet)
- {
- CloseADORecordSet(pRSet);
- if( pRSet )
- {
- delete pRSet;
- pRSet = NULL;
- }
- }
- INT Ado2Impl::GetTbl_NetShareInfoRecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT vector<TblNetShareInfo>& vtRSet)
- {
- TCHAR szSQL[1024] = _T("");
- if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
- _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
- else
- _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
- int 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);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- {
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- }
- else
- {
- nRecordCount = -1;
- }
- DeleteADORecordSet(pRSet);
- pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, pSql);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- nFieldCount = pRSet->GetFieldCount();
- for ( int i = 0; i < nRecordCount; i++)
- {
- TblNetShareInfo tagVal;
- if( !pRSet->IsFieldNull(_T("enable")) )
- {
- pRSet->GetFieldValue(_T("enable"), vtFieldValue);
- tagVal._enable = vtFieldValue;
- }
- else
- {
- tagVal._enable = 0;
- }
- if( !pRSet->IsFieldNull(_T("branchid")) )
- {
- pRSet->GetFieldValue(_T("branchid"), vtFieldValue);
- tagVal._branchid = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._branchid = _T("");
- }
- if( !pRSet->IsFieldNull(_T("sharePath")) )
- {
- pRSet->GetFieldValue(_T("sharePath"), vtFieldValue);
- tagVal._sharepath = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._sharepath = _T("");
- }
- if( !pRSet->IsFieldNull(_T("mincapacity")) )
- {
- pRSet->GetFieldValue(_T("mincapacity"), vtFieldValue);
- tagVal._mincapacity = vtFieldValue;
- }
- else
- {
- tagVal._mincapacity = 0;
- }
- if( !pRSet->IsFieldNull(_T("maxcapacity")) )
- {
- pRSet->GetFieldValue(_T("maxcapacity"), vtFieldValue);
- tagVal._maxcapacity = vtFieldValue;
- }
- else
- {
- tagVal._maxcapacity = 0;
- }
- if( !pRSet->IsFieldNull(_T("photoType")) )
- {
- pRSet->GetFieldValue(_T("photoType"), vtFieldValue);
- tagVal._phototype = vtFieldValue;
- }
- else
- {
- tagVal._phototype = 0;
- }
- if( !pRSet->IsFieldNull(_T("priority")) )
- {
- pRSet->GetFieldValue(_T("priority"), vtFieldValue);
- tagVal._priority = vtFieldValue;
- }
- else
- {
- tagVal._priority = 0;
- }
- vtRSet.push_back(tagVal);
- pRSet->MoveNext();
- }
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
- INT Ado2Impl::GetTbl_dindanRecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT vector<Tbldindan>& vtRSet)
- {
- TCHAR szSQL[1024] = _T("");
- if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0)
- _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName);
- else
- _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters);
- int 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);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, szSQL);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- if( !pRSet->IsFieldNull(_T("cot")) )
- {
- pRSet->GetFieldValue(_T("cot"), nRecordCount);
- }
- else
- {
- nRecordCount = -1;
- }
- DeleteADORecordSet(pRSet);
- pRSet = new CADORecordset(m_pADODatabase);
- LeaveCriticalSection( &m_csAdo );
- if( NULL == pRSet )
- {
- return -1;
- }
- bResult = OpenADORecordSet(pRSet, pSql);
- if( false == bResult )
- {
- DeleteADORecordSet(pRSet);
- return -1;
- }
- nFieldCount = pRSet->GetFieldCount();
- for ( int i = 0; i < nRecordCount; i++)
- {
- Tbldindan tagVal;
- if( !pRSet->IsFieldNull(_T("id")) )
- {
- pRSet->GetFieldValue(_T("id"), vtFieldValue);
- tagVal._id = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._id = _T("");
- }
- if( !pRSet->IsFieldNull(_T("name1")) )
- {
- pRSet->GetFieldValue(_T("name1"), vtFieldValue);
- tagVal._name1 = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._name1 = _T("");
- }
- if( !pRSet->IsFieldNull(_T("name2")) )
- {
- pRSet->GetFieldValue(_T("name2"), vtFieldValue);
- tagVal._name2 = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._name2 = _T("");
- }
- if( !pRSet->IsFieldNull(_T("taoxiname")) )
- {
- pRSet->GetFieldValue(_T("taoxiname"), vtFieldValue);
- tagVal._taoxiname = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._taoxiname = _T("");
- }
- if( !pRSet->IsFieldNull(_T("taoxijiage")) )
- {
- pRSet->GetFieldValue(_T("taoxijiage"), vtFieldValue);
- tagVal._taoxijiage = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._taoxijiage = _T("");
- }
- if( !pRSet->IsFieldNull(_T("curno")) )
- {
- pRSet->GetFieldValue(_T("curno"), vtFieldValue);
- tagVal._curno = TString((TCHAR*)(_bstr_t)vtFieldValue);
- }
- else
- {
- tagVal._curno = _T("");
- }
- vtRSet.push_back(tagVal);
- pRSet->MoveNext();
- }
- DeleteADORecordSet(pRSet);
- return nRecordCount;
- }
|