Recently, we are using the mfc odbc Class to develop a database application, because we have never used the mfc odbc Class. I have paid a lot of effort for this. In addition, many new users are asking related questions in CSDN and other communities, so that they do not forget their troubles. So I found a place and wrote it down for query.
1. CDatabase Class and CRecordset Class can be directly used without having to derive new class
2. When using the CRecordset class directly, it is best to declare a CDatabase object to manually open the database. Do not directly use the CRecordset
3. Do not load the dynamic cursor library when you use CDatabase to open the database, because this will block some functions of the corresponding database driver. After useCursorLib is loaded, you cannot use dynamic datasets (dynaset). You can only use static datasets (snapshot ). However, static datasets reduce the efficiency of recording a large amount of data in databases, because static datasets are used to maintain a static image of data. Uses temporary data as an image for datasets returned from SQL and query. A large number of records will reduce the efficiency.
Note that the Open () and OpenEx () member functions of CDatabase are used. The last parameter of the former is to load the dynamic cursor library by default.
Therefore, the error "dynamic record set is not supported" is caused by this.
4. When using the VC Class Wizard to create a dataset class whose base class is CRecordset, pay attention to whether the data type of the member class used by the Class Wizard when binding fields matches your database field type. one. I found that the Class Wizard does not define the data type of the bound Variable Based on the Data Type of the field itself. A common id or number field in our database is often processed in the long type. In fact, such fields are usually in the dense type. At this time, no error occurs. The error only jumps out when you execute an SQL query.
When executing an SQL query, if two errors occur: "invalid descriptor Index" and "retrieval record error", most of them are caused by inconsistent database fields with the Data Type of the bound variables. The former is generally caused by a static dataset (snapshot), while the latter is generally caused by a dynamic dataset (dynaset.
5. There is no need to use the Class Wizard to generate the database category class (CDatabase) and dataset class (CRecordse), in addition to bringing convenience of data binding. It also brings about the disadvantages that a database can only correspond to one dataset class, because data binding exists and you cannot use this class in databases with different structures. Therefore, using the CRecordset dataset class directly seems to be the best choice. You can use the GetODBCFieldInfo () and GetFieldValue () CRecordset member functions to access database records instead of Binding data provided by MFC.
GetODBCFieldInfo // obtain the information of a specified field, such as the field type and width.
GetFieldValue // get the content of the specified field in the current record
With these two functions, we can easily obtain the Record Content and this dataset class can adapt to any database. Of course, in this case, we can only access data. If we want to write data, it will not work. However, we can use SQL statements to write data.
See: http://winu.cn/htmls/400/062/