Solved

How to catch ODBC failure ?

Posted on 1998-10-22
2
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
msa092298 earned 100 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

636 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