MFC: Connecting SQL Server with ADO

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.