The following program takes all data from a table called
TestTable
in the database
TestDB
and displays to the user
Here you need to replace the Data Source(Your server name), Initial Catalog(your DB name) and table name
#include "stdafx.h"
#include <stdio.h>
#include <ole2.h>
#include <iostream>
#import "C:/Program Files/Common Files/System/ado/msado15.dll" rename( "EOF", "AdoNSEOF" )
using namespace std;
using namespace ADODB;
_bstr_t bstrConnect("Provider='SQLOLEDB'; Data Source='PC001\\SQLEXPRESS'; Initial Catalog='TestDB'; Integrated Security='SSPI';");
int main()
{
HRESULT hResult = CoInitialize( 0 );
if( FAILED( hResult ))
{
return hResult;
}
try
{
ADODB::_ConnectionPtr pConnect("ADODB.Connection");
hResult = pConnect->Open( bstrConnect, "admin", "", ADODB::adConnectUnspecified );
if (SUCCEEDED(hResult))
{
_bstr_t query = "SELECT * FROM TestTable;";
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 hResult;
}