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 (const CString 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 ("Move recordset pointer to header Error"));
if (precordset->getstate ()! = adstateclosed)
{
hr = Precordset->close ();
if (FAILED (HR))
{
M_plogfile->writelog (GetLastError (), _t ("Failed to close Recordset"));
}
Else
{
M_plogfile->writelog (GetLastError (), _t ("Recordset closed");
}
}
if (precordset! = NULL)
{
Precordset.release ();
Precordset = NULL;
}
LeaveCriticalSection (&m_cs);
return FALSE;
}
}
Else
{
M_plogfile->writelog (GetLastError (), _t ("Query Recordset is empty");
}
}
Else
{
M_plogfile->writelog (GetLastError (), _t ("Failed to open record set");
if (precordset! = NULL)
{
Precordset.release ();
Precordset = NULL;
}
LeaveCriticalSection (&m_cs);
return FALSE;
}
}
Else
{
M_plogfile->writelog (GetLastError (), _t ("initializing Recordset Failed");
Precordset = NULL;
LeaveCriticalSection (&m_cs);
return FALSE;
}
}
catch (_com_error e)
{
M_plogfile->writelog (E.errormessage ());
AfxMessageBox (E.errormessage ());
LeaveCriticalSection (&m_cs);
return FALSE;
}
if (precordset->getstate ()! = adstateclosed)
{
Try
{
hr = Precordset->close ();
if (FAILED (HR))
{
M_plogfile->writelog (GetLastError (), _t ("Failed to close Recordset"));
}
Else
{
M_plogfile->writelog (GetLastError (), _t ("Recordset closed");
}
}
catch (_com_error err)
{
M_plogfile->writelog (Err.errormessage ());
AfxMessageBox (Err.errormessage ());
}
}
if (precordset! = NULL)
{
Precordset.release ();
Precordset = NULL;
}
LeaveCriticalSection (&m_cs);
return TRUE;
}
Database calling code
if (! Database.query (_t ("Select ZoneName from Zone"), 1, Vecveczone))
{
Logfile.writelog (GetLastError (), _t ("Failed to get client name from database");
AfxMessageBox (_t ("Failed to get client name from database"));TOEFL Answers
Return
}
Error issues
The program generated an exception in query. In the call Precordset->open (... ), an exception is generated, and the exception error message is "Unspecified error";
Placing the invoked SQL statement in the Access database will execute correctly.IELTS Answer
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.
ADO Accessing Access Database error resolution