本文執行個體講述了VC基於ADO技術訪問資料庫的方法。分享給大家供大家參考。具體如下:
一、在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();}
希望本文所述對大家的VC程式設計有所協助。