Procedure for connecting to the ms SQL Server database (pubs) using ADO

Source: Internet
Author: User

Procedure for connecting to the ms SQL Server database (pubs) using ADO
1: ADO inherits ole db, while ole db is compiled based on com technology. So ADO itself is also a COM component.

2: Create a basic dialog box
In stdafx. h
// Import the dynamic link library and change the end ID to rsEOF
# Import "D:/Program files/Common Files/System/ado/msado15.dll" no_namespace rename ("EOF", "rsEOF ")

2: drag a list box control on the interface and name it IDC_LIST1.
Add a Code such
// Initialization
CoInitialize (NULL );
// Define
_ ConnectionPtr pConn (_ uuidof (Connection ));
_ RecordsetPtr pRst (_ uuidof (Recordset ));
// Connection string
PConn-> ConnectionString = "Provider = SQLOLEDB.1; Password = 123456q; Persist SecurityInfo = True; User ID = sa; Initial Catalog = pubs ";
// Enable (synchronize)
PConn-> Open ("", adConnectUnspecified );
// Execute
PRst = pConn-> Execute ("select * from authors", NULL, ad1_text );

While (! PRst-> rsEOF)
{
(CListBox *) GetDlgItem (IDC_LIST1)-> AddString (_ bstr_t) pRst-> GetCollect ("au_lname "));
PRst-> MoveNext (); // move the subscript down
}

PRst-> Close (); // Close the record set
PConn-> Close (); // Close the connection

PRst. Release (); // Release the reference count of the smart pointer on the com interface, that is, Release the record set and connection object.
PConn. Release ();
CoUninitialize (); // uninstall the com Library

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.