使用VC++创建一个基于对话框的MFC应用程序
在stdafx.h头文件中引入 msado15.dll文件
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")文件的路径
View Code
void CADODlg::OnBnClickedBtnquery() { // TODO: 在此添加控件通知处理程序代码 CoInitialize(NULL); _ConnectionPtr pConn(__uuidof(Connection)); _RecordsetPtr pRst(__uuidof(Recordset)); _CommandPtr pCmd(__uuidof(Command)); pConn->ConnectionString="Provider=SQLOLEDB.1;Server=.;DATABASE=Users;UID=sa;PWD=***;"; pConn->Open("","","",adConnectUnspecified); //pRst=pConn->Execute("select * from [User]",NULL,adCmdText); //pRst->Open("select * from [User]",_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText); pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn)); pCmd->CommandText="select * from [User]"; pRst=pCmd->Execute(NULL,NULL,adCmdText); while(!pRst->adoEOF) { ((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Name")); pRst->MoveNext(); } pRst->Close(); pConn->Close(); pCmd.Release(); pRst.Release(); pConn.Release(); CoUninitialize(); }