# include "iostream. H "
# include" stdio. H "
# import" C:/program files/common files/system/ADO/msado15.dll "no_namespace Rename (" EOF "," adoeof ")
int main (INT argc, char * argv [])
{< BR >:: coinitialize (null);
_ connectionptr m_pconnection;
m_pconnection.createinstance ("ADODB. connection ");
try
{< br> m_pconnection-> open (" provider = Microsoft. jet. oledb.4.0; Data Source = database // mydb. mdb "," "," ", admodeunknown);
// M_pconnection-> open ("Server = 192.168.0.150, 1433; uid = sa; Pwd = 123456; database = temp; provider = sqloledb ;","","", -1); // connect to SQL Server
}
Catch (_ com_error E)
{
Cout <"database connection failed. Check whether mydb. mdb is in the current path! "<Endl;
Return false;
}
_ Recordsetptr m_precordset;
M_precordset.createinstance ("ADODB. recordset ");
Try
{
M_precordset-> open ("select * from test ",
M_pconnection.getinterfaceptr (),
Adopendynamic,
Adlockoptimistic,
Adcmdtext );
}
Catch (_ com_error * E)
{
Cout <e-> errormessage () <Endl;
}
_ Variant_t var;
Char * ID, * Name;
Try
{
If (! M_precordset-> BOF)
M_precordset-> movefirst ();
Else {
Cout <"table data is empty" <Endl;
Return 1;
}
While (! M_precordset-> adoeof)
{
Var = m_precordset-> getcollect ("ID ");
If (var. VT! = Vt_null)
Id = _ com_util: convertbstrtostring (_ bstr_t) var );
Var = m_precordset-> getcollect ("name ");
If (var. VT! = Vt_null)
Name = _ com_util: convertbstrtostring (_ bstr_t) var );
Cout <id <"is" <name <Endl;
M_precordset-> movenext ();
}
}
Catch (_ com_error * E)
{
Cout <e-> errormessage () <Endl;
}
M_precordset-> close ();
M_precordset = NULL;
If (m_pconnection-> state)
M_pconnection-> close ();
M_pconnection = NULL;
Return 0;
}