Connect to mysql using VC ++ ADO

Source: Internet
Author: User
Through self-exploration and online help to learn how VC uses ADO to connect to the mysql database: the method used is to use ADO to establish an ODBC data source to ultimately access MySQL. 1. Install the mysqldata warehouse server program and install appserv-win32-2.5.9.rar which contains appche, php, and mysql. The latter can be installed on the webpage.

Through self-exploration and online help to learn how VC uses ADO to connect to the mysql database: the method used is to use ADO to establish an ODBC data source to ultimately access MySQL. 1. Install the mysqldata warehouse server program and install appserv-win32-2.5.9.rar which contains appche, php, and mysql. The latter can be installed on the webpage.

I learned how to connect to the mysql database using ADO through self-exploration and online help:
You can use ADO to establish an ODBC data source to access MySQL.
1. Install the mysqldata warehouse server program and install appserv-win32-2.5.9.rar which contains appche, php, and mysql. After installation, you can manage databases on the web page. It is convenient for me to use the latter.

Remember the password during installation.
2. Create a database:
You can create an http: // localhost/phpMyAdmin/user name and password on the Web page to manage the database on the web page.
You can import databases:
-- PhpMyAdmin SQL Dump
-- Version 2.10.2
Http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Date: July 24, 2009
-- Server version: 5.0.22
-- PHP version: 5.2.3

SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO ";

--
-- Database: 'testmysql'
--

----------------------------------------------------------

--
-- Table structure 'test'
--

Create table 'test '(
'Id' int (4) not null auto_increment,
'Name' varchar (50) not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = gbk AUTO_INCREMENT = 3;

--
-- Export the table data 'test'
--

Insert into 'test' VALUES (1, 'qq ');
Insert into 'test' VALUES (2, 'ww ');

3. Install MyODBC I installed: MyODBC-3.51.11-2-win.exe
Then, choose Start> Settings> Control Panel> Administrative Tools> data source (ODBC)> system DSN> Add> MySQL ODBC 3.51 Driver.

You can enter all the data here to enter the data in the program. Here, DSN is ADOmysql.
4. If all the preparations have been completed, we can start to write a program to connect to the database:
Program code: code source: Visual C ++ SQL Server database application instance full Parsing

1 // ADOConn. h: interface for the ADOConn class.
2 //
3 /**//////////////////////////////////// ///////////////////////////////////
4 # import "c: \ Program Files \ Common Files \ System \ ado \ msado15.dll" no_namespace rename ("EOF", "adoEOF") rename ("BOF", "adoBOF ")
5
6 # if! Defined (afx_adoconn_h1_ac448f02_af26_45e4_9b2d_d7ecb8ffcfb91_encoded _)
7 # define afx_adoconn_h1_ac448f02_af26_45e4_9b2d_d7ecb8ffcfb91_encoded _
8
9 # if _ MSC_VER> 1000
10 # pragma once
11 # endif // _ MSC_VER> 1000
12
13 class ADOConn
14 {
15 // define variables
16 public:
17 // Add a pointer to the Connection object:
18 _ ConnectionPtr m_pConnection;
19 // Add a pointer to the Recordset object:
20 _ RecordsetPtr m_pRecordset;
21 // definition method
22 public:
23 ADOConn ();
24 virtual ~ ADOConn ();
25 // initialization-connect to the database
26 void OnInitADOConn ();
27 // execute the query
28 _ RecordsetPtr & GetRecordSet (_ bstr_t bstrSQL );
29 // execute the SQL statement, Insert Update _ variant_t
30 BOOL ExecuteSQL (_ bstr_t bstrSQL );
31 void ExitConnect ();
32 };
33
34 # endif //! Defined (afx_adoconn_h1_ac448f02_af26_45e4_9b2d_d7ecb8ffcfb91_encoded _)
35


1 // ADOConn. cpp: implementation of the ADOConn class.
2 //
3 /**//////////////////////////////////// ///////////////////////////////////
4
5 # include "stdafx. h"
6 # include "ADOConn. h"
7
8 # ifdef _ DEBUG
9 # undef THIS_FILE
10 static char THIS_FILE [] =__ FILE __;
11 # define new DEBUG_NEW
12 # endif
13
14 /**//////////////////////////////////// ///////////////////////////////////
15 // Construction/Destruction
16 /**//////////////////////////////////// ///////////////////////////////////
17
18 ADOConn: ADOConn ()
19 {
20
21}
22
23 ADOConn ::~ ADOConn ()
24 {
25
26}
27
28 // initialization-connect to the database
29 void ADOConn: OnInitADOConn ()
30 {
31 // initialize the OLE/COM library Environment
32: CoInitialize (NULL );
33
34 try
35 {
36 // create a Connection object
37 m_pConnection.CreateInstance ("ADODB. Connection ");
38 m_pConnection-> Open ("DSN = ADOmysql; Server = localhost; Database = testmysql", "root", "123456", adModeUnknown );
39}
40 // capture exceptions
41 catch (_ com_error e)
42 {
43 // Display error message
44 AfxMessageBox (e. Description ());
45}
46}
47
48 // execute the query
49_RecordsetPtr & ADOConn: GetRecordSet (_ bstr_t bstrSQL)
50 {
51 try
52 {
53 // connect to the database. If the Connection object is null, reconnect to the database.
54 if (m_pConnection = NULL)
55 OnInitADOConn ();
56 // create a record set object
57 m_pRecordset.CreateInstance (_ uuidof (Recordset ));
58 // retrieve records from the table
59 m_pRecordset-> Open (bstrSQL, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adshorttext );
60}
61 // capture exceptions
62 catch (_ com_error e)
63 {
64 // Display error message
65 AfxMessageBox (e. Description ());
66}
67 // return record set
68 return m_pRecordset;
69}
70
71 // execute the SQL statement, Insert Update _ variant_t
72 BOOL ADOConn: ExecuteSQL (_ bstr_t bstrSQL)
73 {
74 // _ variant_t RecordsAffected;
75 try
76 {
77 // whether the database has been connected
78 if (m_pConnection = NULL)
79 OnInitADOConn ();
80 // Execute method of the Connection object :( _ bstr_t CommandText,
81 // VARIANT * RecordsAffected, long Options)
82 // CommandText is a command string, usually an SQL command.
83 // The RecordsAffected parameter indicates the number of rows affected after the operation is completed,
84 // The Options parameter indicates the CommandText type: adCmdText-text command; adCmdTable-Table Name
85 // adw.proc-stored procedure; adCmdUnknown-Unknown
86 m_pConnection-> Execute (bstrSQL, NULL, ad1_text );
87 return true;
88}
89 catch (_ com_error e)
90 {
91 AfxMessageBox (e. Description ());
92 return false;
93}
94}
95
96 void ADOConn: ExitConnect ()
97 {
98 // close the record set and Connection
99 if (m_pRecordset! = NULL)
100 m_pRecordset-> Close ();
101 m_pConnection-> Close ();
102 // release the environment
103: CoUninitialize ();
104}
Create an object for this class and then you can access the mysql database:
1 ADOConn m_ADO;
2 m_ADO.OnInitADOConn ();
3
4 // set the SELECT statement
5_bstr_t vSQL;
6 vSQL = "SELECT * FROM test WHERE id = 1 ";
7 // execute the SELETE statement
8 _ RecordsetPtr m_pRecordset;
9 m_pRecordset = m_ADO.GetRecordSet (vSQL );
10
11 CString name0;
12 // return the values of each column
13 if (! M_pRecordset-> adoEOF)
14 {
15
16 name0 = (LPCTSTR) (_ bstr_t) m_pRecordset-> GetCollect ("name ");
17}
18 // disconnect from the database
19 m_ADO.ExitConnect ();
20
21 MessageBox (name0, L "id = 1", 0 );
Running result:

Http://hi.baidu.com/blueliche_n/item/02c888ffbc45245ac9f337b2

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.