How to restore the database link by using ADO to connect the database, the problem of disconnection and reconnection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.