#include "StdAfx.h" #include "ADODatabase.h" BOOL CADODatabase::Execute(LPCTSTR lpstrExec) { ASSERT(m_pConnection != NULL); ASSERT(_tcscmp(lpstrExec, _T("")) != 0); _variant_t vRecords; m_nRecordsAffected = 0; try { m_pConnection->CursorLocation = adUseClient; m_pConnection->Execute(_bstr_t(lpstrExec), &vRecords, adExecuteNoRecords); m_nRecordsAffected = vRecords.iVal; 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(_tcscmp(lpstrConnection, _T("")) != 0) m_strConnection = lpstrConnection; ASSERT(!m_strConnection.IsEmpty()); try { if (m_nConnectionTimeout != 0) m_pConnection->PutConnectionTimeout(m_nConnectionTimeout); hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL); return hr == S_OK; } 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(_T("CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription ); m_strErrorDescription = (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(); }