Solved

How to catch ODBC failure ?

Posted on 1998-10-22
2
365 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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