Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to catch ODBC failure ?

Posted on 1998-10-22
2
Medium Priority
?
385 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 400 total points
ID: 1323706
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
ID: 1323707
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

963 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