C++簡單練習

C++MFC用ADO連接MSSQL:

 

在stdafx.h中倒入ado.dll:

#import "C:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","rsEOF")//倒入ADO動態連接庫並做設置

 

按鈕事件代碼:

try
 {
  CoInitialize(NULL);
  _ConnectionPtr pConn(__uuidof(Connection));
  _RecordsetPtr pRst(__uuidof(Recordset));
  pConn->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PassWord=123;Server=.;Initial Catalog=WirelessCare";
  pConn->Open("","","",adConnectUnspecified);
  pRst=pConn->Execute("select * from City",NULL,adCmdText);
  while(!pRst->rsEOF)
  {
   ((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("name"));
   pRst->MoveNext();
  }
  pRst->Close();
  pConn->Close();
  pRst.Release();
  pConn.Release();
  CoUninitialize();
 }
 catch(_com_error e)
 {
   AfxMessageBox(e.Description());
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章