Microsoft Developer Studio provides a 32-bit ODBC drive for most standard database formats. These standard data formats include: SQL Server, Access, Paradox, DBASE, FoxPro, Excel, Oracle, and Microsoft Text. If the user wishes to use other data formats, you need to install the appropriate ODBC drive and DBMS. Once users use their own DBMS database management feature to generate new database modes, you can log in to the data source using ODBC. For users' applications, you can register a lot of different databases as long as the driver is installed. See the specific operations of the login database See online help for ODBC. I. The MFC base class library of the ODBC database class Visual C provided by the MFC defines several database classes. When programming with ODBC, you are often used to use CDATABASE, CRecordSet (Record Class), and CRecordView. The CDATABASE class object provides a connection to the data source, and it can operate the data source by it. The CRecordset class object provides a recordset extracted from the data source. CRecordset objects are usually used in two forms: dynamic rows (Dynastes) and snapshots. Dynamic row can keep synchronization with other users, and the snapshot set is a static view of the data. Each form provides a set of records when the recording set is opened, which is different. When scrolling into a record in a dynamic row, other records in other users or applications are done. Changes will be displayed accordingly. The CRecordView class object displays database records in the form of controls. This view is a table view that is directly connected to a CRecordset object. Second, Application ODBC Programming Applications Visual C AppWizard can automatically generate an ODBC application framework, the steps are: Open the New option of the File menu, select Projects, fill in the engineering name, select MFC AppWizard (EXE), then press AppWizard to make a prompt operating. When AppWizard is included in the database support, if you want to read and write the database, select Database View With File Support; if you want to access the database, you don't want to write back the changes, then select Database View WITHOUT FILE SUPPORT. After selecting the database support, the Database Source button will be activated and select it to call the Data Options dialog. In the Database Options dialog, the database resources that have been registered with ODBC are displayed, and the database you want to operate is selected, such as: Super_ES, click OK to appear Select Database Tables dialog, which lists all the tables contained in the selected database; After selecting the table you want to operate, click OK. After selecting the database and data table, you can continue the appWizard operation in accordance with the convention. It is important to indicate that in the generated application framework View class (such as: CSUPER_ESVIEW) contains a pointer M_Pset pointing to the CSUPER_ESSET object, the pointer is established by AppWizard, the purpose is to establish a connection between the viewing form and the recordset, make The query results in the record set can be easily displayed on the view form. To make the program to establish a contact with the data source, you need to initialize with the cdatebase :: OpenEx () or CDATABASE :: Open () function. Database objects must be initialized before using it constructs record set objects.
Third, Example 1. Query Record Query Record Using CRecordset :: Open () and CRecordset :: REQUERY () Member Functions. Before using the CRecordset class object, you must use the CRecordset :: Open () function to get a valid recordset. Once you have used the CRecordset :: Open () function, you can apply the CRecordset :: Requery () function when you query. When calling the CRecordset :: Open () function, if you pass an open CDatabase object to the M_PDatabase member variable that you have passed to the CRecordset class object, use the database object to create an ODBC connection; otherwise, if M_PDATABASE is an empty pointer, you will create a new CDATABASE. Class objects and make it connected to the default data source and then perform the initialization of CRecordset class objects. The default data source is obtained by a getDefaultconnect () function. You can also provide the required SQL statement, and call the CRecordset :: Open () function, for example: super_esset.open (AFX_DATABASE_USE_DEFAULT, STRSQL); if no parameters are specified, the program uses the default SQL statement, ie The SQL statement specified in the getDefaultsql () function operates: CString Csuper_esset :: getDefaultsql () {return_t ("[bsicdata], [minsize]");} The table name returned for the getDefaultsql () function, the corresponding default operation Is the SELECT statement, namely: select * from BasicData, MAINSIZE can also utilize CRecordset member variables M_Strfilter and M_STRSORT to perform conditional query and results sort. M_Strfilter is a filter string, stores the condition string after WHERE in the SQL statement; m_strsort is the sort string, stores the string after the ORDER BY in the SQL statement. Such as: Super_ESSet.m_strFilter = "TYPE = 'motor'"; Super_ESSet.m_strSort = "VOLTAGE"; Super_ESSet.Requery (); corresponding to the SQL statement: SELECT * FROM BasicData, MainSize WHERE TYPE = 'motor' ORDER BY VOLTAGE addition Direct assignment to m_strfilter, you can use parameterization. Using parameterization can be more intuitive and more convenient to complete the conditional query task. The steps to use parameterization are as follows: S declares the parameters:
CSTRING P1; FLOAT P2; S Initialize the amount of parameters in the constructor: p1 = _t (""); p2 = 0.0f; m_nparams = 2; s binds the parameter with the corresponding column: PFX-> setfieldType (cfieldexchange :: PARAM) RFX_Text (PFX, _T ("P1"), P1); RFX_SINGLE (PFX, _T ("P2"), P2); After completing the above steps, you can use the parameters to check: m_pset-> m_strfilter = "TYPE =? And voltage =? "; M_pset-> p1 =" electric motor "; m_pset-> p2 = 60.0; m_pset-> Requory (); the value of the parameter is replaced in the query string in the order of the binding order"? "Wildcard . If the result of the query is multiple records, you can use the CRecordset class to move the cursor with the CRecordset class. 2. Increase the record to increase the record using the addNew () function, require the database to open: m_pset-> addnew (); // Add new record m_pset-> setfieldnull at the end of the table, add new record m_pset-> setfieldnull (m_pset-> m_type ), False); m_pset-> m_type = "electric motor"; ... // Enter new field value m_pset-> update (); // store new records into database m_pset-> request (); // Rebuild record set 3. Deleting records can be used directly to delete records, and do not call Update () functions after calling the delete () function: m_pset-> delete (); if (! M_pset-> iesof ()) m_pset- > MOVENEXT (); Else M_Pset-> MoveLast (); 4. Modify Record Modify Record Using Edit () Functions: m_pset-> Edit (); // Modify The current record m_pset-> m_type = "generator"; // Modify The current record field value ... m_pset-> update (); // store the modification result into the database m_pset-> Requory (); 5. Undo operation If you select an increase or modify the record, you want to give up the current operation, you can call Update () Call: CRecordset :: move (AFX_MOVE_REFRESH) to undo increase or modify the mode and restore the current record before adding or modifying the mode. Where the parameter AFX_MOVE_REFRESH is zero. 6. Database connection DEREGY CRECORDSET class defines a member variable m_pdatabase: cdatabase * m_pdatabase; it is a pointer to the object database class. If you pass a open CDatabase class object norm to M_PDatabase, you can share the same CDatabase class object before the CRecordset class object calls the Open () function.
Such as: cdatabase m_db; crecordset m_set1, m_set2; m_db.open (_t ("super_es")); // Establish an ODBC connection m_set1.m_pdatabase = & m_db; // m_set1 multiplex m_db object m_set2.m_pdatabase = & m_db; // m_set2 complex Direct execution of the m_db object 7.sql statement Although we can do most query operations through the CRecordset class, and in the CRecordset :: Open () function, you can also provide SQL statements, but sometimes we still want to do some other operations. For example, create a new table, delete a table, create a new field, etc., then you need to use the CDATABASE class to directly execute the SQL statement. To complete the SQL statement by calling CDatabase :: ExecuteSQL () function directly execute: BOOL CDB :: ExecuteSQLAndReportFailure (const CString & strSQL) {TRY {m_pdb-> ExecuteSQL (strSQL); // execute SQL statements directly} CATCH (CDBException, e ) {Cstring strmsg; strmsg.loadstring (IDS_EXECUTE_SQL_FAILED); strmsg = strsql; return false;} End_catch return true;} It should be noted that due to the different DBMS provided by different DBMS, directly execute SQL statements may undermine Software's DBMS irrelevant, so this type of operation should be used with caution in the application. 8. Dynamic connection of dynamic connection tables can be implemented using the SQL statement when calling the CRecordset :: Open () function. The same record set object can only access tables with the same structure, otherwise the query result will not correspond to the variable. Void CDB :: ChangeTable () {if (m_pset-> isopen ()) m_pset-> close (); switch (m_id) {case 0: m_pset-> open (AFX_DB_USE_DEFAULT_TYPE, "SELECT * from slam slot0"); // Connection Table slot0 m_id = 1; Break; Case 1: m_pset-> open (AFX_DB_USE_DEFAULT_TYPE, "Select * from slot1"); // Connection table slot1 m_id = 0; Break;}} 9. Dynamic connection database can pass the CRecordset class Object parameters m_pdatabase to connect to the CDatabase object pointer of different databases to implement dynamic connection databases.