Connect to the ACCESS database using ADO in VC

Source: Internet
Author: User

Connect to the ACCESS database using ADO in VC
 
# Include <iostream. h>
# Import "C:/program files/common files/system/ADO/msado15.dll" no_namespace Rename ("EOF", "adoeof ")

Int main (){

// Use ADO to connect to the database...
//---------------------------------------------------------------------------------
_ Connectionptr m_pconnection;

Coinitialize (null );
M_pconnection.createinstance (_ uuidof (connection ));

// In the ADO operation, we recommend that you use try... catch () to capture error information,
// Because it often produces unexpected errors.
Try
{
// Open the local access database db1.mdb
M_pconnection-> open ("provider = Microsoft. Jet. oledb.4.0; Data Source = db1.mdb", "", "", admodeunknown );

}
Catch (_ com_error E)
{
Cout <"database connection failed. Check whether db1.mdb is in the current path! "<Endl;
Return false;
}
// Configure //-------------------------------------------------------------------------------------
// Create a dataset
// Configure //-------------------------------------------------------------------------------------
_ Recordsetptr m_precordset;
M_precordset.createinstance (_ uuidof (recordset ));

// In the ADO operation, we recommend that you use try... catch () to capture error information,
// Because it often produces unexpected errors.
Try
{
M_precordset-> open ("select * From home2 ",
M_pconnection.getinterfaceptr (), // gets the idispatch pointer of the database connected to the database
Adopendynamic,
Adlockoptimistic,
Adcmdtext );
}
Catch (_ com_error * E)
{
// Afxmessagebox (e-> errormessage ());
Cout <e-> errormessage () <Endl;
}

// Configure //--------------------------------------------------------------------------------------
// Read data
// Configure //--------------------------------------------------------------------------------------
_ Variant_t var;
Char * Strid, * strx, * stry;
Float X, Y;
Try
{
If (! M_precordset-> BOF)
M_precordset-> movefirst ();
Else {
Cout <"table data is empty" <Endl;
Return 1;
}

// Read the fields in the database and add them to the list box.
While (! M_precordset-> adoeof)
{
Var = m_precordset-> getcollect ("ID ");
If (var. VT! = Vt_null)
Strid = _ com_util: convertbstrtostring (_ bstr_t) var); // _ variant_t to convert string
Var = m_precordset-> getcollect ("X ");
If (var. VT! = Vt_null)
Strx = _ com_util: convertbstrtostring (_ bstr_t) var );
Var = m_precordset-> getcollect ("Y ");
If (var. VT! = Vt_null)
Stry = _ com_util: convertbstrtostring (_ bstr_t) var );
Cout <Strid <"is" <strx <"" <stry <Endl;

M_precordset-> movenext ();
}
}
Catch (_ com_error * E)
{
Cout <e-> errormessage () <Endl;
}

// Configure //--------------------------------------------------------------------------------------
// Close the dataset
M_precordset-> close ();
M_precordset = NULL;
// Configure //--------------------------------------------------------------------------------------
// Close the database connection
// Configure //--------------------------------------------------------------------------------------
If (m_pconnection-> state)
M_pconnection-> close ();
M_pconnection = NULL;
Return 0;
}

The above are the source code. It is strange to have a problem. The original name of my database is position, but it cannot be connected. I just changed it to home2, I don't know what's going on?

 

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.