It is easy connecting using ADO. see the below code. It displays all the fields in a table called
Annount
from the access database
MyDB
#include <iostream>
#include <tchar.h>
#import <C:\\Program Files\\Common Files\\System\\ado\\msado15.dll> \
rename( "EOF", "AdoNSEOF" )
using namespace std;
_bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\MyTrainingDB\\MyDB.accdb;";
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hResult = CoInitialize( 0 );
if( FAILED( hResult ))
{
return 0;
}
try
{
ADODB::_ConnectionPtr pConnect("ADODB.Connection");
hResult = pConnect->Open( bstrConnect, "admin", "", ADODB::adConnectUnspecified );
if (SUCCEEDED(hResult))
{
_bstr_t query = "SELECT * FROM Account;";
ADODB::_RecordsetPtr pRecSet( "ADODB.Recordset" );
hResult = pRecSet->Open( query, _variant_t((IDispatch *) pConnect, true), ADODB::adOpenUnspecified,
ADODB::adLockUnspecified, ADODB::adCmdText);
if( SUCCEEDED( hResult ))
{
ADODB::Fields* pFields = NULL;
hResult = pRecSet->get_Fields( &pFields );
while( !pRecSet->AdoNSEOF )
{
for( long lIndex=0; lIndex < pFields->GetCount(); lIndex++ )
{
cout<<" | ";
cout << _bstr_t(pFields->GetItem(lIndex)->GetValue());
}
cout<< "\n----------------------------------------------------------------------------\n";
pRecSet->MoveNext();
}
}
pRecSet->Close();
pConnect->Close();
}
}
catch( _com_error& e )
{
}
CoUninitialize();
getchar();
return 0;
}</tchar.h></iostream>