简体   繁体   中英

Checking SQL connection state in ADO

I am checking my SQL connection state as:

_ConnectionPtr          m_pADOConnection; 

// Connection is created and working fine...

// Now I disable network adapter (from Control panel)

if( (pApp->m_pConnection->GetState() == adStateOpen)  )
{
    // I got here every time....
}

The problem is I get every time adStateOpen even if connection is really not working!

If I try to execute a query or do anything it fails, mostly with

SMux Provider: Physical connection is not usable [xFFFFFFFF].

or

Error number: 80004005 = Unable to open a logical session

Is this value of State property reliable or I need to perform some other check top detect this state?

The state property does not become 0 (adStateClosed) when the connection is interrupted. So checking the state of the connection will always return 1 (adStateOpen) even after the connection is interrupted.

No there is no way to check immediately. The architecture of SQL servers don't allow for it.

I advise you to create an error handling.

It looks as though the only way to establish that the connection has been lost is to try to open it and handle the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM