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?