ADOConnection.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:4k
源码类别:

网格计算

开发平台:

Visual C++

  1. #include "ADOConnection.h"
  2. //Initialize or UnInitialize the ADO connection.
  3. bool CADOConnection::ADO_Initialize()
  4. {
  5. return !FAILED(::CoInitialize(NULL));
  6. }
  7. void CADOConnection::ADO_UnInitialize()
  8. {
  9. ::CoUninitialize();
  10. }
  11. // Construction/Destruction
  12. CADOConnection::CADOConnection()
  13. {
  14. if( !CADOConnection::ADO_Initialize() ){
  15. throw new CADOException( "CADOConnection::CADOConnection", "ADO_Initialize() Failed." );
  16. }
  17. this->m_bConnected = false;
  18. this->m_pConnection = NULL;
  19. this->m_sConnectionString.erase();
  20. //Create the connection object
  21. try{
  22. HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");
  23. if(FAILED(hr)){
  24. throw new CADOException( "CADOConnection::CADOConnection", "Create ADO Connection Instance Failed." );
  25. }
  26. }
  27. catch(_com_error e){
  28. throw new CADOException( "CADOConnection::CADOConnection", e.Description() );
  29. }
  30. }
  31. CADOConnection::~CADOConnection()
  32. {
  33. this->Destroy();
  34. }
  35. //get the connection smart ptr.
  36. _ConnectionPtr CADOConnection::getConnectionPtr()
  37. {
  38. return this->m_pConnection;
  39. }
  40. //Set the connection string and connecting mode.
  41. bool CADOConnection::Create(LPCSTR lpszConnect,
  42.    ConnectModeEnum cmMode,
  43.    CursorLocationEnum clCursorLocation,
  44.    int nConnectionTimeOut)
  45. {
  46. if( !_STREMPTY(lpszConnect) )
  47. {
  48. //store the connection string.
  49. this->m_sConnectionString = lpszConnect;
  50. try{
  51. //set the connection properties.
  52. m_pConnection->CursorLocation = clCursorLocation;
  53. this->m_bConnected = _COMOK(
  54. m_pConnection->Open(_bstr_t(lpszConnect), "", "", cmMode)
  55. );
  56. return this->m_bConnected;
  57. }
  58. catch(_com_error e){
  59. throw new CADOException( "CADOConnection::Create", e.Description() );
  60. }
  61. }
  62. return false;
  63. }
  64. //destroy the connection.
  65. void CADOConnection::Destroy()
  66. {
  67. try{
  68. //close the db connection
  69. if(this->IsActived()){
  70. this->m_pConnection->Close();
  71. this->m_pConnection = NULL;
  72. this->m_sConnectionString.erase();
  73. this->m_bConnected  = false;
  74. }
  75. }
  76. catch(_com_error e){
  77. throw new CADOException( "CADOConnection::Destroy", e.Description() );
  78. }
  79. }
  80. //get the connection whether is opened.
  81. bool CADOConnection::IsActived()
  82. {
  83. return this->m_bConnected;
  84. }
  85. //get the connection string.
  86. LPCSTR CADOConnection::GetConnectionString(void)
  87. {
  88. return this->m_sConnectionString.data();
  89. }
  90. //Transaction processing.
  91. bool CADOConnection::BeginTransaction()
  92. {
  93. try{
  94. return SUCCEEDED( m_pConnection->BeginTrans() );
  95. }
  96. catch(_com_error e){
  97. throw new CADOException( "CADOConnection::BeginTransaction", e.Description() );
  98. }
  99. return false;
  100. }
  101. bool CADOConnection::CommitTransaction()
  102. {
  103. try{
  104. return SUCCEEDED( m_pConnection->CommitTrans() );
  105. }
  106. catch(_com_error e){
  107. throw new CADOException( "CADOConnection::CommitTransaction", e.Description() );
  108. }
  109. return false;
  110. }
  111. bool CADOConnection::RollBackTransaction()
  112. {
  113. try{
  114. return SUCCEEDED( m_pConnection->RollbackTrans() );
  115. }
  116. catch(_com_error e){
  117. throw new CADOException( "CADOConnection::RollBackTransaction", e.Description() );
  118. }
  119. return false;
  120. }
  121. //Execute some sql statements.
  122. _RecordsetPtr CADOConnection::Execute(LPCSTR lpszSQLText, CommandTypeEnum ctCmdType)
  123. {
  124. _variant_t vRecAffected;
  125. vRecAffected.Clear();
  126. //m_pConnection->CommandTimeout = 0;
  127. return this->m_pConnection->Execute(
  128. _bstr_t(lpszSQLText), &vRecAffected, ctCmdType);
  129. }
  130. //open schema.
  131. _RecordsetPtr CADOConnection::OpenSchema(SchemaEnum Schema)
  132. {
  133. _RecordsetPtr rs = NULL;
  134. // Get a list of tables and dump list to console.
  135. rs = m_pConnection->OpenSchema( Schema );
  136. return rs;
  137. }