ADOCommand.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. m_nRecordsAffected = 0;
  13. }
  14. BOOL CADOCommand::AddParameter(CADOParameter* pAdoParameter)
  15. {
  16. ASSERT(pAdoParameter->GetParameter() != NULL);
  17. try
  18. {
  19. m_pCommand->Parameters->Append(pAdoParameter->GetParameter());
  20. return TRUE;
  21. }
  22. catch(_com_error& e)
  23. {
  24. dump_com_error(e);
  25. return FALSE;
  26. }
  27. }
  28. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, int nValue)
  29. {
  30. _variant_t vtValue;
  31. vtValue.vt = VT_I2;
  32. vtValue.iVal = nValue;
  33. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  34. }
  35. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, long lValue)
  36. {
  37. _variant_t vtValue;
  38. vtValue.vt = VT_I4;
  39. vtValue.lVal = lValue;
  40. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  41. }
  42. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, double dblValue, int nPrecision, int nScale)
  43. {
  44. _variant_t vtValue;
  45. vtValue.vt = VT_R8;
  46. vtValue.dblVal = dblValue;
  47. return AddParameter(strName, nType, nDirection, lSize, vtValue, nPrecision, nScale);
  48. }
  49. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, CString strValue)
  50. {
  51. _variant_t vtValue;
  52. vtValue.vt = VT_BSTR;
  53. vtValue.bstrVal = strValue.AllocSysString();
  54. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  55. }
  56. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, COleDateTime time)
  57. {
  58. _variant_t vtValue;
  59. vtValue.vt = VT_DATE;
  60. vtValue.date = time;
  61. return AddParameter(strName, nType, nDirection, lSize, vtValue);
  62. }
  63. BOOL CADOCommand::AddParameter(CString strName, int nType, int nDirection, long lSize, _variant_t vtValue, int nPrecision, int nScale)
  64. {
  65. try
  66. {
  67. _ParameterPtr pParam = m_pCommand->CreateParameter(strName.AllocSysString(), (DataTypeEnum)nType, (ParameterDirectionEnum)nDirection, lSize, vtValue);
  68. pParam->PutPrecision(nPrecision);
  69. pParam->PutNumericScale(nScale);
  70. m_pCommand->Parameters->Append(pParam);
  71. return TRUE;
  72. }
  73. catch(_com_error& e)
  74. {
  75. dump_com_error(e);
  76. return FALSE;
  77. }
  78. }
  79. void CADOCommand::SetText(CString strCommandText)
  80. {
  81. ASSERT(!strCommandText.IsEmpty());
  82. m_strCommandText = strCommandText;
  83. m_pCommand->CommandText = m_strCommandText.AllocSysString();
  84. }
  85. void CADOCommand::SetType(int nCommandType)
  86. {
  87. m_nCommandType = nCommandType;
  88. m_pCommand->CommandType = (CommandTypeEnum)m_nCommandType;
  89. }
  90. BOOL CADOCommand::Execute(int nCommandType /*= typeCmdStoredProc*/)
  91. {
  92. _variant_t vRecords;
  93. m_nRecordsAffected = 0;
  94. try
  95. {
  96. m_nCommandType = nCommandType;
  97. m_pCommand->Execute(&vRecords, NULL, nCommandType);
  98. m_nRecordsAffected = vRecords.iVal;
  99. return TRUE;
  100. }
  101. catch(_com_error &e)
  102. {
  103. dump_com_error(e);
  104. return FALSE;
  105. }
  106. }
  107. void CADOCommand::dump_com_error(_com_error &e)
  108. {
  109. CString ErrorStr;
  110. _bstr_t bstrSource(e.Source());
  111. _bstr_t bstrDescription(e.Description());
  112. ErrorStr.Format(_T("CADOCommand Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n"),
  113. e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription );
  114. m_strLastError = ErrorStr;
  115. m_dwLastError = e.Error();
  116. #ifdef _DEBUG
  117. AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR);
  118. #endif
  119. }