VC使用ADO技术访问数据库

    技术2022-05-11  13

    一、在StdAfx.h文件中添加#import "C:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","rsEOF")导入ADO引擎。二、数据库应用层操作void CADOExample1Dlg::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=123456;Persist Security Info=True;User ID=sa;Initial Catalog=db_test;Data Source=."; pConn->Open("","","",adConnectUnspecified); pRst = pConn->Execute("select * from tb_image",NULL,adCmdText); while(!pRst->rsEOF) { ((CListBox*)GetDlgItem(IDC_LIST1))->AddString( (_bstr_t)pRst->GetCollect("imageID")); pRst->MoveNext(); } pRst->Close(); pConn->Close(); pRst.Release(); pConn.Release(); CoUninitialize(); }


    最新回复(0)