The way to use is to constantly query the database connection status with a timer,
void Csqldlg::ontimer (UINT nidevent) { // todo:add Your message handler code here and/or call Defau Lt if(nidevent==timer_sql) { // check SQL m_bsql= Cmakesuresql::check (g_connection); Here's the global cadoconnection* if(! M_bsql) { cmakesuresql::retset (g_connection,g_strdatapath); // To reset a database connection
With the new pconn to the old Pconn assignment, this step is critical, if the database is reproduced, if the Open data set error, when the wrong time, you should first check the old
If the pconn is invalid, the address of the old and new Pconn is different, then the old connection is invalidated.
}
}
The check function here is
BOOL Cmakesuresql::check (cadoconnection*&pconn) {Null_returnfalse (pconn); This is a macro definition, written by yourself, BOOL BRet=FALSE; Cadorecordset*Pdataset; Try{ if(Pconn! =NULL) {Pdataset=NewCadorecordset; Pdataset-setadoconnection (Pconn); } if(pdataset->IsOpen ()) {Pdataset-Close (); } CString strSQL="Select GETDATE ()"; BRet=pdataset->openrecordset (strSQL)! =adores_failed; Use this to test if the database connection is broken.Catch(...) { //DT ("Cmakesuresql test data connection is abnormal!");bret=FALSE; } safe_delete (Pdataset); returnBRet;}
BOOL Cmakesuresql::retset (cadoconnection*& pconn,cstring& strconn) { if(pconn!= NULL) { pconn-Disconnect (); Safe_delete (pconn); } New cadoconnection; Null_returnfalse (pconn); = pconn->Connect (strconn); //
if (bres==returnreturn
}
These are program fragments, lack of corresponding code under the premise, and can not run, used to inspire ideas with,
How to restore the database link by using ADO to connect the database, the problem of disconnection and reconnection