ADODatabase.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "StdAfx.h"
  2. #include "ADODatabase.h"
  3. BOOL CADODatabase::Execute(LPCTSTR lpstrExec)
  4. {
  5. ASSERT(m_pConnection != NULL);
  6. ASSERT(_tcscmp(lpstrExec, _T("")) != 0);
  7. _variant_t vRecords;
  8. m_nRecordsAffected = 0;
  9. try
  10. {
  11. m_pConnection->CursorLocation = adUseClient;
  12. m_pConnection->Execute(_bstr_t(lpstrExec), &vRecords, adExecuteNoRecords);
  13. m_nRecordsAffected = vRecords.iVal;
  14. return TRUE;
  15. }
  16. catch(_com_error &e)
  17. {
  18. dump_com_error(e);
  19. return FALSE;
  20. }
  21. }
  22. DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs)
  23. {
  24. DWORD numRows = 0;
  25. numRows = m_pRs->GetRecordCount();
  26. if(numRows == -1)
  27. {
  28. if(m_pRs->EndOfFile != VARIANT_TRUE)
  29. m_pRs->MoveFirst();
  30. while(m_pRs->EndOfFile != VARIANT_TRUE)
  31. {
  32. numRows++;
  33. m_pRs->MoveNext();
  34. }
  35. if(numRows > 0)
  36. m_pRs->MoveFirst();
  37. }
  38. return numRows;
  39. }
  40. BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
  41. {
  42. HRESULT hr = S_OK;
  43. if(IsOpen())
  44. Close();
  45. if(_tcscmp(lpstrConnection, _T("")) != 0)
  46. m_strConnection = lpstrConnection;
  47. ASSERT(!m_strConnection.IsEmpty());
  48. try
  49. {
  50. if (m_nConnectionTimeout != 0)
  51. m_pConnection->PutConnectionTimeout(m_nConnectionTimeout);
  52. hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
  53. return hr == S_OK;
  54. }
  55. catch(_com_error &e)
  56. {
  57. dump_com_error(e);
  58. return FALSE;
  59. }
  60. }
  61. void CADODatabase::dump_com_error(_com_error &e)
  62. {
  63. CString ErrorStr;
  64. _bstr_t bstrSource(e.Source());
  65. _bstr_t bstrDescription(e.Description());
  66. ErrorStr.Format(_T("CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n"),
  67. e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription );
  68. m_strErrorDescription = (LPCSTR)bstrDescription ;
  69. m_strLastError = _T("Connection String = " + GetConnectionString() + '\n' + ErrorStr);
  70. m_dwLastError = e.Error();
  71. #ifdef _DEBUG
  72. AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR );
  73. #endif
  74. }
  75. BOOL CADODatabase::IsOpen()
  76. {
  77. if(m_pConnection )
  78. return m_pConnection->GetState() != adStateClosed;
  79. return FALSE;
  80. }
  81. void CADODatabase::Close()
  82. {
  83. if(IsOpen())
  84. m_pConnection->Close();
  85. }