Monday, May 5, 2008

VC中如何使用ADO?

#include "stdafx.h"
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
int main(int argc, char* argv[])
{
printf("Use ADO to open C:\\tmp\\test.mdb database file!\n");
CoInitialize(NULL);
try
{
_ConnectionPtr pConn("ADODB.Connection");
_RecordsetPtr pRst("ADODB.Recordset");
pConn->Open("PROVIDER=MSDASQL;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\tmp\\test.mdb;UID=;PWD=aaa;"

//pConn->Open("Provider= Microsoft.Jet.OLEDB.4.0;Data Source=C:\\tmp\\test.mdb;"//this is also OK
,"","",adConnectUnspecified);
//Open "users" table
pRst->Open("users", _variant_t((IDispatch *) pConn, true),
adOpenStatic, adLockReadOnly, adCmdTable);
FieldsPtr fds=pRst->GetFields();
printf("printf field name of the table\n");
for(int i=0;iGetCount();i++)
{
FieldPtr fd=fds->GetItem(_variant_t(short(i)));
printf("%s ",(LPCTSTR)fd->GetName());
}
printf("\n");
pRst->Close();
pConn->Close();
}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
::CoUninitialize();
return 0;
}

No comments: