ADO Accessing Access Database error resolution

Source: Internet
Author: User

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

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.