Connect to the SQL Server database using ADO

Source: Internet
Author: User
Tags ole visual studio 2010

This article does not introduce how ADO connects to the database, but describes the connection steps and simple methods.

1. Obtain the connection string.

The _ connectionptr object open method is required for database connection. The parameters are as follows:

Hresult connection15: open (_ bstr_t connectionstring, _ bstr_t userid, _ bstr_t password, _ bstr_t options)

Some people feel very difficult when writing this connectionstring connection string. The following is a simple method to obtain this connection string.

1.1 create a. udl file, open it, select Microsoft ole db provider for SQL Server in the program options provided, and then click Next. For example:


1.2 fill in necessary information in the connection and click test connection. If the connection is successful, a prompt box is displayed, as shown in.


The server name is the name of your SQL Server server. If you do not know it, you can see it in the server properties of SQL Server, such:

2. the console program is posted below to describe the connection process. Compiling environment: Visual Studio 2010 flagship Note: 1. If garbled characters are found in the execution results, set the character set item in the project property to multi-byte character set or not set. 2. Note that the connection string in the program is suitable for my computer database. If you want to test it, modify it for yourself (you can obtain the connection string according to the above method)
// Import An ADO dynamic link library msado15.dll # import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll" # include <stdio. h> inline void testhr (hresult X) {if failed (x) _ com_issue_error (x) ;}; void connectionstringx (_ connectionptr pconnection ); // connect to the database _ bstr_t getstate (INT intstate); // obtain the connection status void printprovidererror (_ connectionptr pconnection); // Output Error void printcomerror (_ com_error & E ); // Output Error void exitconnect (_ connectionptr pconnection); // close the connection void main () {// initialize the OLE/COM library environment if (failed (:: coinitialize (null) {return ;}// create the connection object _ connectionptr pconnection = NULL; connectionstringx (pconnection); exitconnect (pconnection); // release the environment :: couninitialize ();} void connectionstringx (_ connectionptr pconnection) {hresult hR = s_ OK; try {// ADO connection to the database testhr (pconnection. createinstance (_ uuidof (connection); pconnection-> connectionstring = "provider = 'sqloledb'; Data Source = 'sd-20120421uvit \ sqlexpress '; "" Initial catalog = 'master'; Integrated Security = 'sspi '; "; pconnection-> connectiontimeout = 10; pconnection-> open ("","","", adconnectunspecified); printf ("connection1 state: % s \ n", (lpcstr) getstate (pconnection-> State);} catch (_ com_error & E) {// print the error printprovidererror (pconnection); printcomerror (e) ;}// get the connection status _ bstr_t getstate (INT intstate) {_ bstr_t strstate; Switch (intstate) {Case adstateclosed: strstate = "adstateclosed"; break; Case adstateopen: strstate = "adstateopen"; break; default:;} return strstate ;} // print the error message void printprovidererror (_ connectionptr pconnection) during the connection {// print the error errorptr Perr = NULL in the connection object; if (pconnection-> errors-> count)> 0) {long ncount = pconnection-> errors-> count; For (long I = 0; I <ncount; I ++) {Perr = pconnection-> errors-> getitem (I); printf ("error number: % x \ t % s \ n", Perr-> number, (lpcstr) Perr-> description) ;}}// print the com error void printcomerror (_ com_error & E) {_ bstr_t bstrsource (E. source (); _ bstr_t bstrdescription (E. description (); printf ("error \ n"); printf ("\ tcode = % 08lx \ n", E. error (); printf ("\ tcode meaning = % s \ n", E. errormessage (); printf ("\ tsource = % s \ n", (lpcstr) bstrsource); printf ("\ tdescription = % s \ n", (lpcstr) bstrdescription);} // close the connection void exitconnect (_ connectionptr pconnection) {If (pconnection) if (pconnection-> state = adstateopen) pconnection-> close ();}

The result is as follows:


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.