1. New dialog box: Ado,: Control ListBox and Button
2. Import the ADO library: Import the library in stdafx.h
1 " C:\Program Files\Common Files\system\ado\msado15.dll " no_namespace rename ("EOF","rseof")
3. Query Button code:
1 voidcadodlg::onbtnquery ()2 {3 //Todo:add your control notification handler code here4CoInitialize (NULL);//Initializing COM Libraries5_connectionptr Pconn (__uuidof (Connection));//define an ADO Connection object: Pconn, and initialize6 _recordsetptr pRst (__uuidof (Recordset));7 _commandptr pcmd (__uuidof (Command));8 9Pconn->connectionstring ="Provider=sqloledb; Server=.; Database=infodata; Uid=sa; pwd=12345678;";//"=" cannot have spaces on either sideTenPconn->open (Pconn->connectionstring,"","", adconnectunspecified); One A //pRst = Pconn->execute ("SELECT * from T_check_sn_list", null,adcmdtext); - //prst->open ("SELECT * from T_check_sn_list", _variant_t (idispatch*) pconn), Adopendynamic,adlockoptimistic, adCmdText); -Pcmd->put_activeconnection (_variant_t (idispatch*) (Pconn)) ; thePcmd->commandtext ="SELECT * from T_check_sn_list"; -PRst = pcmd->Execute (null,null,adcmdtext); - while(!prst->rseof) { -((clistbox*) GetDlgItem (idc_list1))->addstring ((_bstr_t) Prst->getcollect ("SeqNum"));//Seqnum is a column of table T_check_sn_list +Prst->MoveNext (); - } +Prst->Close (); APconn->Close (); at pcmd.release (); - prst.release (); - pconn.release (); -CoUninitialize ();//Uninstalling the COM library -}
Success:
MFC: Connecting SQL Server with ADO