MFC:用ADO串連SQLSERVER

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   os   sp   for   

1.建立一對話方塊:Ado,:控制項listbox和button

2.匯入ADO庫:在stdafx.h中匯入該庫

1 #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")

3.查詢按鈕代碼:

 1 void CAdoDlg::OnBtnQuery()  2 { 3     // TODO: Add your control notification handler code here 4     CoInitialize(NULL);    //初始化COM庫 5     _ConnectionPtr pConn(__uuidof(Connection));    //定義一個ADO Connection對象:pConn,並初始化 6     _RecordsetPtr pRst(__uuidof(Recordset)); 7     _CommandPtr pCmd(__uuidof(Command)); 8  9     pConn->ConnectionString = "Provider=SQLOLEDB; Server=.; Database=InfoData; uid=sa; pwd=12345678;";    //"="兩邊不能有空格10     pConn->Open(pConn->ConnectionString,"","",adConnectUnspecified);11 12     //pRst = pConn->Execute("select * from t_check_sn_list",NULL,adCmdText);13     //pRst->Open("select * from t_check_sn_list",_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText);14     pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));15     pCmd->CommandText = "select * from t_check_sn_list";16     pRst = pCmd->Execute(NULL,NULL,adCmdText);17     while(!pRst->rsEOF){18         ((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("SeqNum"));    //SeqNum為表t_check_sn_list的一列19             pRst->MoveNext();20     }21     pRst->Close();22     pConn->Close();23     pCmd.Release();24     pRst.Release();25     pConn.Release();26     CoUninitialize();    //卸載COM庫27 }

成功:

MFC:用ADO串連SQLSERVER

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.