MFC connects MySQL via ODBC (using VS2012 to write MFC)

Source: Internet
Author: User
Tags dsn odbc connection

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.

  

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.