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