標籤: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