ADOBaseImpl.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "StdAfx.h"
  2. #include "ADOBaseImpl.h "
  3. CADOBaseImpl::CADOBaseImpl(void)
  4. {
  5. m_bOpen = FALSE;
  6. m_pADODatabase = NULL;
  7. memset(m_szConnectString,0,1024);
  8. InitializeCriticalSection(&m_csAdo);
  9. }
  10. CADOBaseImpl::~CADOBaseImpl(void)
  11. {
  12. DeleteCriticalSection(&m_csAdo);
  13. }
  14. BOOL CADOBaseImpl::OpenCatalog(IN STDatabaseInfo& dbInfo)
  15. {
  16. if ( m_pADODatabase ) return TRUE;
  17. BOOL bRet = FALSE;
  18. if ( _ttoi(dbInfo.szDatabaseTCPPort) == 0 )
  19. {
  20. _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
  21. dbInfo.szDataSource, // IP華硊麼督昢靡備;
  22. dbInfo.szDatabaseName,
  23. dbInfo.szDatabaseAccount,
  24. dbInfo.szDatabasePassword);
  25. }
  26. else
  27. {
  28. _stprintf_s(m_szConnectString, _T("Provider=sqloledb;Data Source=%s,%s;Initial Catalog=%s;User Id=%s;Password=%s; "),
  29. dbInfo.szDataSource, // IP華硊麼督昢靡備;
  30. dbInfo.szDatabaseTCPPort,
  31. dbInfo.szDatabaseName,
  32. dbInfo.szDatabaseAccount,
  33. dbInfo.szDatabasePassword);
  34. }
  35. m_pADODatabase = new CADODatabase();
  36. if ( m_pADODatabase )
  37. {
  38. m_pADODatabase->SetConnectionString(m_szConnectString);
  39. if ( m_pADODatabase->Open() )
  40. {
  41. bRet = TRUE;
  42. }
  43. else
  44. {
  45. if ( m_pADODatabase->IsOpen() )
  46. {
  47. m_pADODatabase->Close();
  48. }
  49. delete m_pADODatabase;
  50. m_pADODatabase = NULL;
  51. }
  52. }
  53. return bRet;
  54. }
  55. void CADOBaseImpl::CloseCatalog()
  56. {
  57. if( NULL != m_pADODatabase )
  58. {
  59. if( m_pADODatabase->IsOpen() )
  60. {
  61. m_pADODatabase->Close();
  62. }
  63. if( m_pADODatabase )
  64. delete m_pADODatabase;
  65. m_pADODatabase = NULL;
  66. }
  67. }
  68. INT CADOBaseImpl::Execute(IN LPCTSTR lpExcSQL)
  69. {
  70. INT nRetCode = 0;
  71. if ( NULL == m_pADODatabase || NULL == lpExcSQL)
  72. return -1;
  73. try
  74. {
  75. EnterCriticalSection(&m_csAdo);
  76. if ( m_pADODatabase->IsOpen() )
  77. {
  78. nRetCode = m_pADODatabase->Execute(lpExcSQL);
  79. LeaveCriticalSection(&m_csAdo);
  80. }
  81. else
  82. {
  83. LeaveCriticalSection(&m_csAdo);
  84. return -1;
  85. }
  86. }
  87. catch (_com_error* e)
  88. {
  89. }
  90. return nRetCode;
  91. }
  92. INT CADOBaseImpl::GetTblRecordCount(IN LPCTSTR lpTblName, IN LPCTSTR lpFilters)
  93. {
  94. if ( lpTblName == NULL)
  95. return -1;
  96. TCHAR szSQL[1024] = _T("");
  97. if(lpFilters == NULL || _tcscmp(lpFilters,_T("")) == 0)
  98. _stprintf_s(szSQL,_T("select count(*) as cot from %s"),lpTblName);
  99. else
  100. _stprintf_s(szSQL,_T("select count(*) as cot from %s where %s"),lpTblName,lpFilters);
  101. DWORD nRecordCount = 0;
  102. _variant_t vtFieldValue;
  103. BOOL bResult = FALSE;
  104. EnterCriticalSection(&m_csAdo);
  105. bResult = GetADODateBaseStatus();
  106. if( FALSE == bResult )
  107. {
  108. LeaveCriticalSection(&m_csAdo);
  109. return -1;
  110. }
  111. CADORecordset* pRSet = new CADORecordset(m_pADODatabase);
  112. if( NULL == pRSet )
  113. {
  114. LeaveCriticalSection(&m_csAdo);
  115. return -1;
  116. }
  117. bResult = OpenADORecordSet(pRSet, szSQL);
  118. if( false == bResult )
  119. {
  120. LeaveCriticalSection(&m_csAdo);
  121. DeleteADORecordSet(pRSet);
  122. return -1;
  123. }
  124. if( !pRSet->IsFieldNull(_T("cot")) )
  125. {
  126. pRSet->GetFieldValue(_T("cot"), nRecordCount);
  127. }
  128. else
  129. {
  130. nRecordCount = -1;
  131. }
  132. DeleteADORecordSet(pRSet);
  133. LeaveCriticalSection(&m_csAdo);
  134. return nRecordCount;
  135. }
  136. BOOL CADOBaseImpl::IsOpen()
  137. {
  138. return m_pADODatabase == NULL;
  139. }
  140. BOOL CADOBaseImpl::GetADODateBaseStatus()
  141. {
  142. if( NULL == m_pADODatabase || !m_pADODatabase->IsOpen())
  143. return FALSE;
  144. return TRUE;
  145. }
  146. BOOL CADOBaseImpl::OpenADORecordSet(IN CADORecordset *pRSet, IN LPCTSTR lpSQL)
  147. {
  148. if( NULL == pRSet || NULL == lpSQL)
  149. return FALSE;
  150. INT nRetCode;
  151. BOOL bResult = TRUE;
  152. try
  153. {
  154. nRetCode = pRSet->Open(lpSQL, CADORecordset::openQuery);
  155. if (nRetCode == 0)
  156. {
  157. DeleteADORecordSet(pRSet);
  158. bResult = FALSE;
  159. }
  160. }
  161. catch (_com_error &e)
  162. {
  163. DeleteADORecordSet(pRSet);
  164. bResult = FALSE;
  165. }
  166. return bResult;
  167. }
  168. void CADOBaseImpl::CloseADORecordSet(IN CADORecordset* pRSet)
  169. {
  170. if( NULL != pRSet )
  171. {
  172. if( pRSet->IsOpen() )
  173. {
  174. pRSet->Close();
  175. }
  176. }
  177. }
  178. void CADOBaseImpl::DeleteADORecordSet(IN CADORecordset* pRSet)
  179. {
  180. CloseADORecordSet(pRSet);
  181. if( pRSet )
  182. {
  183. delete pRSet;
  184. pRSet = NULL;
  185. }
  186. }