Note that there are some explanatory texts on larger screens.

plurals
  1. POReconnect to MS SQL when connection is lost using MFC?
    primarykey
    data
    text
    <p>My project is written in C++/MFC, &amp; uses CDatabase &amp; CRecordset to access the database. Occasionally, the MS SQL server may go down, breaking my connection to the database, in which case I need to reconnect. I am finding that detecting a disconnect is very difficult. I run my application, and Stop the MS SQL server to test.</p> <p>The function <code>CDatabase::IsOpen()</code> always reports that the database is open, even after MS SQL has gone down, the connection has been broken, and the MFC has thrown exceptions that say the database has been closed. It is only after I execute CDatabase::Close() in my own code that IsOpen() starts to return 'false'.</p> <p>So I am forced to detect the conditions that require me to close and reopen the database connection. Microsoft throws an object of type CDBException. When I call <code>CDBException::GetErrorMessage()</code>, I am returned a string describing the exception in some detail. However, when I examine the value for CDBException::m_nRetCode, it always contains -1. </p> <p>I would prefer to rely on a well-defined integer value for CDBException::m_nRetCode to determine if the database connection is lost. Instead I look for the string "Communication link failure" in CDBException::m_strError</p> <p>I would strongly prefer not to have to parse the returned error string to determine the exception type. Does anybody know why I am getting the -1 in m_nRetCode? Does anybody know if there's a way to definitively check for a disconnect?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload