Solved

How to catch ODBC failure ?

Posted on 1998-10-22
2
354 Views
Last Modified: 2013-11-20
I am using an ODBC connection to an SQL Server Database in my MFC app (VC++ 5.0 Enterprise). My program is running overnight, so it has to operate totally unattended. I need a way of trapping the error if the SQL DB is down and writing an error message to my app's log file.

How and where can I test if the ODBC connection was successful or failed ? In other words, where to test the result of the GetDefaultConnect procedure ?

Thanks a lot,

Derek
0
Comment
Question by:der_bell
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
msa092298 earned 100 total points
Comment Utility
if you are using MFC CReocrdSet classes, you can catch exceptions of type CDBException and CMemoryException, the error information will be in the member variables.
You should use try..catch around nearly all CRecordSet and CDatabase methods which affect a connection (open, move, delete, close,etc...).
here's and example code
BOOL TryODBC (CDatabase &db,const char *SrcName,const char *UID,const char *pwd)
{
      try
      {
            CString s="";//"ODBC;";
            if (UID && *UID)
            {
                  s+="UID=";       s+=UID;
            }
            if (pwd && *pwd)
            {
                  if (UID && *UID) s+=";";
                  s+="PWD=";       s+=pwd;
            }
            s+=";DSN=";      s+=SrcName;
            if (db.OpenEx(s,CDatabase::useCursorLib | CDatabase::noOdbcDialog | CDatabase::openReadOnly))
                  return TRUE;
      }
      catch( CMemoryException* e )
      {
            m_errNum=3;
            e->Delete();
// log in your file here
            return FALSE;
      }
      catch( CDBException* e )
      {
            m_errNum=1;
            char s[10];     itoa(e->m_nRetCode,s,10);
m_errStr="\nTryODBC error : RetCode = ";                              m_errStr+="\n                  Error = ";                  m_errStr+=e->m_strError;
            m_errStr+="\n      StateNativeOrigin = ";  m_errStr+=e->m_strStateNativeOrigin;
            m_errStr+='\n';
                // log m_errStr here
            e->Delete();
            return FALSE;
      }
      catch( CException* e )
      {
            char s[1000];
            e->GetErrorMessage(s,1000);
            m_errStr=s;
            m_errNum=1;
            e->Delete();
      }
//connect failure
      m_errStr="Couldn't Connect";
      return FALSE;
}

0
 
LVL 1

Expert Comment

by:msa092298
Comment Utility
You should connect to the database source first and on success pass the CDatabase object to the constructor of CRecordSet to use that established connection.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now