One. to install the MYSQL-CONNECTOR-ODBC driver
Two. Configure the data source
Computer Control Panel-management tools-Data source odbc-system dsn-Add-Select the MySQL ODBC Driver just installed
Reference http://www.bitscn.com/pdb/mysql/201407/226195.html
If your computer is Win7 64-bit, you need to use C:\Windows\SysWOW64\odbcad32.exe to add a data source
Reference http://blog.csdn.net/wang379275614/article/details/7803576
Three. Code
1. include header file Afxdb.h
2. Operate the database through the following two classes
CDatabase class http://www.cnblogs.com/good90/archive/2012/03/04/2379371.html
CRecordset class http://blog.csdn.net/elcoteq983/article/details/7000745
Error Highlights:
1. Errors using CRecordset::GetFieldValue: Error C2668
M_rsset.getfieldvalue (0, Val);
Compile-time: Error C2668: ' GetFieldValue ': ambiguous call to overloaded function
should read: M_rsset.getfieldvalue ((short) 0, Val);
Cause: The first parameter of the GetFieldValue function is the short type.
Http://www.cppblog.com/xhubobo/archive/2012/06/29/180769.html
2. invalid field name or field index
When using MFC's Crecord class and its derived classes, if you need to get the value of a field, you need to use the GetFieldValue method.
As far as I know, the GetFieldValue method has 2 overloaded versions, one is GetFieldValue ("field index", "Variable name") and the other is GetFieldValue ("field name", "Variable Name"). If you need to get the values of multiple fields, both methods do not require the order in which the field values are obtained. It is important to note that when using the method of the field index, the index of the field queried with the SELECT statement starts at 0, because the type of the index in the GetFieldValue method is short, and therefore requires (short) 0 to cast 0 to the short type, otherwise the " Error C2668: ' GetFieldValue ': ambiguous call to overloaded function, which is described in another article.
Finally, enter the main content of this article. When using the GetFieldValue method, if you get the field value using the field name, although there are no special requirements for the order, there are many times when you run the program with an "invalid field name or field index" error because the method requires the case of the variable name, and the field name is modified to uppercase. The problem can be resolved, such as GetFieldValue ("RID", Val), and for GetFieldValue ("RID", Val), regardless of whether the field names in the data table are case-sensitive.
Http://www.cppblog.com/xhubobo/archive/2012/06/29/180770.html
MFC connects MySQL via ODBC