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.
- Environment
Win7 x86 system;
VS2012 compiler;
Office2010;
ACCESS2000~ACCESS2003 connection string to establish a database connection.
- Key database Operation code
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;}
- Database Call code
-
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 ; }
- Error issues
- The program generated an exception in query. In the call Precordset->open (...) The exception is generated when the exception error message is "Unspecified error";
- Placing the invoked SQL statement in the Access database will execute correctly.
- Solutions
- Modify the database's table name "Zone" to "MyZone" and other names.
- Summarize
- The table name cannot be "Zone" when programmatically accessing the Access database.
- Code upload
- Download Connection: Http://pan.baidu.com/s/1mgFlOqk