?
Solved

How to catch ODBC failure ?

Posted on 1998-10-22
2
Medium Priority
?
383 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 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

719 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