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