ADOCommand.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "StdAfx.h"
  2. #include "ADOCommand.h"
  3. CADOCommand::CADOCommand(CADODatabase* pAdoDatabase, CString strCommandText, int nCommandType)
  4. {
  5. m_pCommand = NULL;
  6. m_pCommand.CreateInstance(__uuidof(Command));
  7. m_strCommandText = strCommandText;
  8. m_pCommand->CommandText = m_strCommandText.AllocSysString();
  9. m_nCommandType = nCommandType;
  10. m_pCommand->CommandType = (CommandTypeEnum)m_nCommandType;
  11. m_pCommand->ActiveConnection = pAdoDatabase->GetActiveConnection();
  12. }
  13. BOOL CADOCommand::AddParameter(CADOParameter* pAdoParameter)
  14. {
  15. ASSERT(pAdoParameter->GetParameter() != NULL);
  16. try
  17. {
  18. m_pCommand->Parameters->Append(pAdoParameter->GetParameter());
  19. return TRUE;
  20. }
  21. catch(_com_error& e)
  22. {
  23. dump_com_error(e);
  24. return FALSE;
  25. }
  26. }
  27. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, int nValue)
  28. {
  29. _variant_t vtValue;
  30. vtValue.vt = VT_I2;
  31. vtValue.iVal = nValue;
  32. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  33. }
  34. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, long lValue)
  35. {
  36. _variant_t vtValue;
  37. vtValue.vt = VT_I4;
  38. vtValue.lVal = lValue;
  39. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  40. }
  41. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, double dblValue)
  42. {
  43. _variant_t vtValue;
  44. vtValue.vt = VT_R8;
  45. vtValue.dblVal = dblValue;
  46. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  47. }
  48. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, CString strValue)
  49. {
  50. _variant_t vtValue;
  51. vtValue.vt = VT_BSTR;
  52. vtValue.bstrVal = strValue.AllocSysString();
  53. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  54. }
  55. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, COleDateTime time)
  56. {
  57. _variant_t vtValue;
  58. vtValue.vt = VT_DATE;
  59. vtValue.date = time;
  60. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  61. }
  62. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, _variant_t vtValue)
  63. {
  64. try
  65. {
  66. m_pCommand->CreateParameter(strName.AllocSysString(), (DataTypeEnum)nType, (ParameterDirectionEnum)nDirection, lSize, vtValue);
  67. return TRUE;
  68. }
  69. catch(_com_error& e)
  70. {
  71. dump_com_error(e);
  72. return FALSE;
  73. }
  74. }
  75. void CADOCommand::SetText(CString strCommandText)
  76. {
  77. ASSERT(!strCommandText.IsEmpty());
  78. m_strCommandText = strCommandText;
  79. m_pCommand->CommandText = m_strCommandText.AllocSysString();
  80. }
  81. void CADOCommand::SetType(int nCommandType)
  82. {
  83. m_nCommandType = nCommandType;
  84. m_pCommand->CommandType = (CommandTypeEnum)m_nCommandType;
  85. }
  86. void CADOCommand::dump_com_error(_com_error &e)
  87. {
  88. CString ErrorStr;
  89. _bstr_t bstrSource(e.Source());
  90. _bstr_t bstrDescription(e.Description());
  91. ErrorStr.Format( "CADOCommand Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n",
  92. e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
  93. m_strLastError = ErrorStr;
  94. m_dwLastError = e.Error();
  95. #ifdef _DEBUG
  96. AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR);
  97. #endif
  98. }