AdoInterface.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "stdafx.h"
  2. #include "AdoInterface.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #endif
  6. CAdoInterface::CAdoInterface()
  7. {
  8. }
  9. CAdoInterface::~CAdoInterface()
  10. {
  11. CloseDataBase();
  12. }
  13. //************************************//
  14. // [函数]:CreateInstance
  15. // [描述]:初始化ado
  16. // [参数]:
  17. // [返回]:S_OK成功,S_FALSE失败
  18. //************************************//
  19. HRESULT CAdoInterface::CreateInstance()
  20. {
  21. HRESULT hr = CoInitialize(NULL);
  22. if(!SUCCEEDED(hr))
  23. return S_FALSE;
  24. hr = m_pContPtr.CreateInstance(__uuidof(Connection));
  25. if(!SUCCEEDED(hr))
  26. return S_FALSE;
  27. return S_OK;
  28. }
  29. //************************************//
  30. // [函数]:OpenDataBase
  31. // [描述]:打开数据库(如:ACCSESS)
  32. // [参数]:
  33. // pConnectStr: 连接数库的信息;
  34. // pPWD: 数据库密码s
  35. // ResultMsg 返回错误信息
  36. // [返回]:S_OK成功,S_FALSE失败
  37. //************************************//
  38. HRESULT CAdoInterface::OpenDataBase(const TCHAR* pConnectStr, const TCHAR* pUserID, const TCHAR* pPWD, CString& errMsg)
  39. {
  40. if(pConnectStr == NULL || pPWD == NULL)
  41. return S_FALSE;
  42. _variant_t returnValue;
  43. char ErrMessage[512] = {0};
  44. try
  45. {
  46. /*
  47. ACCESS97 Provider=Microsoft.Jet.OLEDB.3.51
  48. ACCESS2000 Provider=Microsoft.Jet.OLEDB.4.0
  49. */
  50. //m_pContPtr->Open(_T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=PhoneBook.mdb"), _T(""), _T(""), adModeUnknown);
  51. m_pContPtr->Open(pConnectStr, pUserID, pPWD, adModeUnknown);
  52. }
  53. catch(_com_error e)
  54. {
  55. /*CString strErr = _T("打开数据库失败:");
  56. strErr += e.ErrorMessage();
  57. strErr += _T(",");
  58. strErr += e.Description().GetBSTR();
  59. WChar2Char(ErrMessage, strErr.GetBuffer());
  60. errMsg = _com_util::ConvertStringToBSTR(ErrMessage);*/
  61. return S_FALSE;
  62. }
  63. return S_OK;
  64. }
  65. //************************************//
  66. // [函数]:CloseDataBase
  67. // [描述]:关闭数据库
  68. // [参数]:
  69. // [返回]:
  70. //************************************//
  71. void CAdoInterface::CloseDataBase()
  72. {
  73. if(m_pContPtr)
  74. {
  75. if(m_pContPtr->State)
  76. m_pContPtr->Close();
  77. m_pContPtr = NULL;
  78. }
  79. }