1.ODBC connecting MySQL
What is ODBC first?
Open Database Interconnect (CONNECTIVITY,ODBC) is an integral part of the database in the Microsoft Open Service Architecture (Wosa,windows Open Services Architecture). It establishes a set of specifications and provides a set of standard APIs (application programming interfaces) for database access. These APIs use SQL to accomplish most of their tasks. ODBC itself also provides support for the SQL language, which allows users to send SQL statements directly to ODBC.
ODBC can be found through the following path: Control Panel, administrative Tools, data Source (ODBC)
1.1 Installing MYSQL-ODBC Driver
This driver connects the MySQL database with ODBC. It can be downloaded from the MySQL website, but it needs to be registered. It is important to note that if you are using vs2012 and a lower vs version, download the 32-bit driver, or the VS will not connect successfully to ODBC. The reason is that VS has only a 32-bit version, reason references-why VS2010 does not have a 64-bit version.
1.2 ODBC connection MySQL
Open ODBC.
Click Add in the User DSN.
Select MySQL ODBC Unicode driver, complete, and the following interface appears.
Name the datasource in data source name; Deccription for description, not filled in, TCP/IP Sever fill in the MySQL user name, password fill in the MySQL password localhost;user. When all of the above are filled in correctly, database can have databases in MySQL database for you to choose from. Then click Test to show that successful is successful.
Then go back to the User DSN interface to see your DSN.
2.MFC program connects to ODBC
2.1 Open the StdAfx.h header file and add the following statement
#include <odbcinst.h>
#include "Afxdb.h"
These two header files define the database operation classes to be used later.
2.2 Opening a database
CDatabase DB; CString sentence;sentence. Format ("Odbc;dsn=sunny; uid=%s; pwd=%s", Mypublicdata.login_name,mypublicdata.login_pass);d B. Open (null,false,false,sentence); if (! db. IsOpen ()) &db); Rs. Open (CRecordset::forwardOnly, _t ("select * from Login_info"));
Where the DSN, UID, and PWD in format correspond to the data Source Name, User, and Password in the ODBC that was built. The SQL statement is self-customizing.
The above code is only the simplest operation to open a database.