Solve the problem of accessing the Access database through ADO

Source: Internet
Author: User
Welcome to the CC ++ programming Community Forum and interact with 3 million technical staff. Recently, a strange error occurred while accessing the Access database using ADO. I think it is necessary to record it, share with you. Environment win7x86 system; VS2012 compiler; Office2010; Access2000 ~ Access2003 connection string to establish database connection

Welcome to the C/C ++ programming Community Forum and interact with 3 million technical staff> A strange error occurred when I recently accessed the Access database using ADO, I think it is necessary to record it and share it with you. Environment: win7 x86; VS2012 compiler; Office2010; Access2000 ~ Access2003 connection string to establish database connection

Welcome to the C/C ++ programming Community Forum and interact with 3 million technical staff> enter

Recently, When I accessed the Access database using ADO, I encountered a strange error. I think it is necessary 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 > & VecvecVariant)

{

_ Variant_t Variant;

Vector <_ variant_t> vecVariant;

Entercriticalkaline (& 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 when moving the record set 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 ("record set disabled "));

}

}

If (pRecordset! = NULL)

{

PRecordset. Release ();

PRecordset = NULL;

}

Leavecriticalkaline (& m_cs );

Return FALSE;

}

}

Else

{

M_pLogFile-> WriteLog (GetLastError (), _ T ("the queried record set is empty "));

}

}

Else

{

M_pLogFile-> WriteLog (GetLastError (), _ T ("failed to open record set "));

If (pRecordset! = NULL)

{

PRecordset. Release ();

PRecordset = NULL;

}

Leavecriticalkaline (& m_cs );

Return FALSE;

}

}

Else

{

M_pLogFile-> WriteLog (GetLastError (), _ T ("failed to initialize record set "));

PRecordset = NULL;

Leavecriticalkaline (& m_cs );

Return FALSE;

}

}

[1] [2]

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.