//DbAccess.cpp #include "DbAccess.h" using namespace std; inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);}; /* * * * * */ void DbAccess::Close(){ ::CoUninitialize(); }//end DbAccess::Close() _RecordsetPtr DbAccess::Execute(_bstr_t strSQL){ m_strErr = ""; m_boolHasError = false; if (m_pConnection == NULL){ if(FAILED(::CoInitialize(NULL))){ m_strErr = "DbAccess::Execute: FAILED(::CoInitialize(NULL))"; return NULL; } try{ // Define string variables. _bstr_t strCnn(m_strConn.c_str()); // Open connection. TESTHR(m_pConnection.CreateInstance(__uuidof(Connection))); m_pConnection->Open (strCnn, "", "", adConnectUnspecified); }catch (_com_error &e){ m_strErr += PrintProviderError(m_pConnection, strSQL); m_strErr += PrintComError(e); m_boolHasError = true; return NULL; } }//end if (m_pConnection == NULL) //printf("%s", (char*)strSQL); //Open and return Recordset try{ return m_pConnection->Execute(strSQL, NULL, adOptionUnspecified); }catch (_com_error &e){ m_strErr += PrintProviderError(m_pConnection, (char*) strSQL); m_strErr += PrintComError(e); m_boolHasError = true; return NULL; } }//end _RecordsetPtr Execute(_bstr_t strSQL) /* * * * * */ string DbAccess::PrintProviderError(_ConnectionPtr pConnection, const char *strSQL){ // Print Provider Errors from Connection object. // pErr is a record object in the Connection's Error collection. ErrorPtr pErr = NULL; string strErrMsg="", strTmp=""; if( (pConnection->Errors->Count) > 0){ long nCount = pConnection->Errors->Count; // Collection ranges from 0 to nCount -1. for(long i = 0; i < nCount; i++){ pErr = pConnection->Errors->GetItem(i); try{ strTmp += "\n\tError number: " + Utils::IntToString(pErr->Number, Utils::enuHex); }catch(...){ strTmp += "\n\tException in pErr->Number"; } try{ strTmp += "\n\t" + pErr->Description; }catch(...){ strTmp += "\n\tException in pErr->Description"; } }//end for if (strTmp != ""){ strErrMsg = "\n*********** BEGIN PrintProviderError:"; strErrMsg += strTmp; strErrMsg += "\n*********** END PrintProviderError"; } return strErrMsg; }//end if return strErrMsg; }//end PrintProviderError(_ConnectionPtr pConnection) /* * * * * */ string DbAccess::PrintComError(_com_error &e){ string strErrMsg="", strTmp=""; _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); // Print Com errors. try{ strTmp += "\n\tError Code: " + Utils::IntToString(e.Error(), Utils::enuHex); }catch(...){ strTmp += "\n\tException in e.Error()"; } try{ strTmp += "\n\tErr Desc: " + (string) e.ErrorMessage(); }catch(...){ strTmp += "\n\tException in e.ErrorMessage()"; } try{ strTmp += "\n\tSource: " + bstrSource; }catch(...){ strTmp += "\n\tException in bstrSource"; } try{ strTmp += "\n\tDescription: " + bstrDescription; }catch(...){ strTmp += "\n\tException in bstrDescription"; } if (strTmp != ""){ strErrMsg = "\n*********** BEGIN PrintComError:"; strErrMsg += strTmp; strErrMsg += "\n*********** END PrintComError"; } return strErrMsg; }//end PrintComError(_com_error &e)