ADODatabase.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*************************************************************
  2. /* Copyright (C), 2014-2020, lyfz. Co., Ltd.
  3. /* 文件名: ADODatabase.h
  4. /* 作者: Jeff.w
  5. /* 创建日期: 2014-08-16
  6. /* 版本号: V1.0
  7. /* 描述: 核心层底,数据库ADO编程
  8. /* 其它:
  9. /* 主要类模块:
  10. /* 历史修改记录:
  11. /* 作者 时间 版本 描述
  12. /* Jeff.w 14/08/18 1.0 创建这个模块
  13. ***************************************************************/
  14. #ifndef __ADO_DATABASE_HEADER__
  15. #define __ADO_DATABASE_HEADER__
  16. #pragma once
  17. #include "AdoDef.h"
  18. class CADODatabase
  19. {
  20. public:
  21. enum cadoConnectModeEnum
  22. {
  23. connectModeUnknown = adModeUnknown,
  24. connectModeRead = adModeRead,
  25. connectModeWrite = adModeWrite,
  26. connectModeReadWrite = adModeReadWrite,
  27. connectModeShareDenyRead = adModeShareDenyRead,
  28. connectModeShareDenyWrite = adModeShareDenyWrite,
  29. connectModeShareExclusive = adModeShareExclusive,
  30. connectModeShareDenyNone = adModeShareDenyNone
  31. };
  32. CADODatabase()
  33. {
  34. ::CoInitialize(NULL);
  35. m_pConnection = NULL;
  36. m_strConnection = _T("");
  37. m_strLastError = _T("");
  38. m_dwLastError = 0;
  39. m_pConnection.CreateInstance(__uuidof(Connection));
  40. m_nRecordsAffected = 0;
  41. m_nConnectionTimeout = 0;
  42. }
  43. virtual ~CADODatabase()
  44. {
  45. Close();
  46. m_pConnection.Release();
  47. m_pConnection = NULL;
  48. m_strConnection = _T("");
  49. m_strLastError = _T("");
  50. m_dwLastError = 0;
  51. ::CoUninitialize();
  52. }
  53. BOOL Execute(LPCTSTR lpstrExec);
  54. BOOL Open(LPCTSTR lpstrConnection = _T(""), LPCTSTR lpstrUserID = _T(""), LPCTSTR lpstrPassword = _T(""));
  55. BOOL IsOpen();
  56. void Close();
  57. // inline func;
  58. inline _ConnectionPtr GetActiveConnection() {return m_pConnection;};
  59. inline int GetRecordsAffected(){return m_nRecordsAffected;};
  60. inline DWORD GetRecordCount(_RecordsetPtr m_pRs);
  61. inline long BeginTransaction() {return m_pConnection->BeginTrans();};
  62. inline long CommitTransaction() {return m_pConnection->CommitTrans();};
  63. inline long RollbackTransaction() {return m_pConnection->RollbackTrans();};
  64. inline void SetConnectionMode(cadoConnectModeEnum nMode){m_pConnection->PutMode((enum ConnectModeEnum)nMode);};
  65. inline void SetConnectionString(LPCTSTR lpstrConnection){m_strConnection = lpstrConnection;};
  66. inline CString GetConnectionString(){return m_strConnection;};
  67. inline CString GetLastErrorString() {return m_strLastError;};
  68. inline DWORD GetLastError(){return m_dwLastError;};
  69. CString GetErrorDescription() {return m_strErrorDescription;};
  70. void SetConnectionTimeout(long nConnectionTimeout = 30) {m_nConnectionTimeout = nConnectionTimeout;};
  71. protected:
  72. void dump_com_error(_com_error &e);
  73. public:
  74. _ConnectionPtr m_pConnection;
  75. protected:
  76. CString m_strConnection;
  77. CString m_strLastError;
  78. CString m_strErrorDescription;
  79. DWORD m_dwLastError;
  80. int m_nRecordsAffected;
  81. long m_nConnectionTimeout;
  82. };
  83. #endif