#include "StdAfx.h" #include "ADODatabase.h" BOOL CADODatabase::Execute(LPCTSTR lpstrExec) { ASSERT(m_pConnection != NULL); ASSERT(strcmp(lpstrExec, _T("")) != 0); try { m_pConnection->CursorLocation = adUseClient; m_pConnection->Execute(_bstr_t(lpstrExec), NULL, adExecuteNoRecords); return TRUE; } catch(_com_error &e) { dump_com_error(e); return FALSE; } } DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs) { DWORD numRows = 0; numRows = m_pRs->GetRecordCount(); if(numRows == -1) { if(m_pRs->EndOfFile != VARIANT_TRUE) m_pRs->MoveFirst(); while(m_pRs->EndOfFile != VARIANT_TRUE) { numRows++; m_pRs->MoveNext(); } if(numRows > 0) m_pRs->MoveFirst(); } return numRows; } BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword) { HRESULT hr = S_OK; if(IsOpen()) Close(); if(strcmp(lpstrConnection, _T("")) != 0) m_strConnection = lpstrConnection; ASSERT(!m_strConnection.IsEmpty()); try { hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL); //BOOL bResult = FALSE; //if(S_OK == hr) //{ // return bResult = TRUE; //} return S_OK == hr; } catch(_com_error &e) { dump_com_error(e); return FALSE; } } void CADODatabase::dump_com_error(_com_error &e) { CString ErrorStr; _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); ErrorStr.Format( "CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n", e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription ); m_strLastError = _T("Connection String = " + GetConnectionString() + '\n' + ErrorStr); m_dwLastError = e.Error(); #ifdef _DEBUG AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR ); #endif } BOOL CADODatabase::IsOpen() { if(m_pConnection ) return m_pConnection->GetState() != adStateClosed; return FALSE; } void CADODatabase::Close() { if(IsOpen()) m_pConnection->Close(); }