To simplify the writing of database applications, Microsoft encapsulates the odbc api interfaces in MFC, which is the mfc odbc and implements the object-oriented database programming interface.
MFC encapsulates ODBC APIs mainly by developing the cdatabase class and the crecordset class.
The cdatabase class is mainly used to establish the connection between applications and data sources.
The crecordset class is used to operate datasets.
Record query: It mainly uses the open () method of the crecordset class and the requery () member function. Before using the crecordset class object, you must use the crecordset member function open () to obtain a valid record set. Once you have used the open () function, you can use the requery () function to query again.
Add record: Only addnew () function is used, but it must be enabled in the allowed mode of the database.
Delete record: the delete () function is called. You do not need to call the update () function after the delete () function is used.
Record modification: after calling the Edit () function, you must call the update () function to save the Modification result to the database.
Undo database update: If you want to undo the original operation after adding, deleting, or editing a record, you should use the move () function before the update () function. Crecordset: Move (afx_move_refresh); this function is used to undo the Add or modify mode and restore the current record before the Add or modify mode. The value of afx_move_refresh is zero.
Directly execute the SQL operation: You need to call the executesql () function of the cdatabase class. It should be noted that because the data operation statements provided by different DBMS are different, direct execution of SQL statements may damage the DBMS independence of the software. Therefore, such operations should be used with caution in applications.
The database operation process of mfc odbc:
First, the application should first establish a connection with the ODBC Data source. This process is implemented using the open () function of the cdatabase class, then the cdatabase class pointer will be passed to the constructor of the crecordset class to combine the crecordset object with the original data source. After connecting the data source, a large number of operations are concentrated on the dataset. After the operation, close the connection to all Record Sets and then close the connection to the data source.