ADO Accessing Access database error resolution Tips

Source: Internet
Author: User
Tags access database

Recently, when accessing the Access database with ADO, there was a strange mistake, and I felt the need to record it and share it with you.

  1. Environment
    1. Win7 x86 system;

    2. VS2012 compiler;

    3. Office2010;

    4. ACCESS2000~ACCESS2003 connection string to establish a database connection.

  2. Key database Operation code
    1. BOOL Caccessdatabase::query (ConstCString strsqlstring, UINT nfieldnumber, vector<vector<_variant_t> >&vecvecvariant)    {_variant_t variant; Vector<_variant_t>vecvariant; EnterCriticalSection (&M_cs); _RecordsetPtr precordset=NULL; HRESULT HR=S_FALSE; Try{hr= Precordset.createinstance (_t ("ADODB. Recordset")); if(SUCCEEDED (HR)) {hr= precordset->Open (strsqlstring.allocsysstring (), M_pconnection.getinterfaceptr (), adOpenDynamic, adLockOptimistic,            adCmdText); if(SUCCEEDED (HR)) {if(!precordset->adoeof) {HR= precordset->MoveFirst (); if(SUCCEEDED (HR)) { while(! (precordset->adoeof))                            {vecvariant.clear ();  for(UINT i =0; i < Nfieldnumber; ++i) {ZeroMemory (&variant,sizeof(Variant)); Variant= Precordset->getcollect (_variant_t (Long) (i));                            Vecvariant.push_back (Variant);                            } vecvecvariant.push_back (Vecvariant); HR= precordset->MoveNext (); }                    }                    Else{m_plogfile->writelog (GetLastError (), _t ("An error occurred while moving the recordset pointer to the header")); if(Precordset->getstate ()! =adstateclosed) {HR= precordset->Close (); if(FAILED (HR)) {M_plogfile->writelog (GetLastError (), _t ("failed to close record set")); }                            Else{m_plogfile->writelog (GetLastError (), _t ("Recordset is closed")); }                        }                        if(Precordset! =NULL)                            {precordset.release (); Precordset=NULL; } leavecriticalsection (&M_cs); returnFALSE; }                }                Else{m_plogfile->writelog (GetLastError (), _t ("the recordset for the query is empty")); }            }            Else{m_plogfile->writelog (GetLastError (), _t ("failed to open record set")); if(Precordset! =NULL)                    {precordset.release (); Precordset=NULL; } leavecriticalsection (&M_cs); returnFALSE; }        }        Else{m_plogfile->writelog (GetLastError (), _t ("initialization of Recordset failed")); Precordset=NULL; LeaveCriticalSection (&M_cs); returnFALSE; }    }    Catch(_com_error e) {m_plogfile-Writelog (E.errormessage ());        AfxMessageBox (E.errormessage ()); LeaveCriticalSection (&M_cs); returnFALSE; }    if(Precordset->getstate ()! =adstateclosed) {        Try{hr= precordset->Close (); if(FAILED (HR)) {M_plogfile->writelog (GetLastError (), _t ("failed to close record set")); }            Else{m_plogfile->writelog (GetLastError (), _t ("Recordset is closed")); }        }        Catch(_com_error err) {M_plogfile-Writelog (Err.            ErrorMessage ()); AfxMessageBox (Err.        ErrorMessage ()); }    }    if(Precordset! =NULL)        {precordset.release (); Precordset=NULL; } leavecriticalsection (&M_cs); returnTRUE;}
  3. Database Call code
    1.  if  (! Database.query (_t ( " select ZoneName from Zone  ), 1   , Vecveczone)) {Logfile.writelog (GetLastError (), _t (  "  Failed to get the client name from the database     "  Failed to get the client name from the database           return  ; }
  4. Error issues
    1. The program generated an exception in query. In the call Precordset->open (...) The exception is generated when the exception error message is "Unspecified error";
    2. Placing the invoked SQL statement in the Access database will execute correctly.
  5. Solutions
    1. Modify the database's table name "Zone" to "MyZone" and other names.
  6. Summarize
    1. The table name cannot be "Zone" when programmatically accessing the Access database.
  7. Code upload
    1. Download Connection: Http://pan.baidu.com/s/1mgFlOqk

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.