在stdafx.h中添加
#import "C:\Program Files\Common Files\System\ado\msador15.dll" no_namespace rename("EOF","rsEOF")
添加代码
void CAdoDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;
User ID=sa;Initial Catalog=pubs";
pConn->Open(" "," "," ",adConnectUnspecified); pRst=pConn->Execute("Select * from authors",NULL,adCmdText); While(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->get_Collect("
au_lname"));
pRst->MoveNext();
} pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}
编译时出错
E:\vc_exercise\Ado\AdoDlg.cpp(177) : error C2065: '_ConnectionPtr' : undeclared identifier
#import "C:\Program Files\Common Files\System\ado\msador15.dll" no_namespace rename("EOF","rsEOF")
添加代码
void CAdoDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;
User ID=sa;Initial Catalog=pubs";
pConn->Open(" "," "," ",adConnectUnspecified); pRst=pConn->Execute("Select * from authors",NULL,adCmdText); While(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->get_Collect("
au_lname"));
pRst->MoveNext();
} pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}
编译时出错
E:\vc_exercise\Ado\AdoDlg.cpp(177) : error C2065: '_ConnectionPtr' : undeclared identifier