(From http://www.j2megame.org/wupei/blog.php/myblog/vc/2007/07/21/vc-ado-access)
Using ADO to directly access the ACCESS database under VC does not require users to establish ODBC data sources)
1. Include the relevant dynamic link library
- // Add the last part in stdafx. H (Note: Make sure it is in the last part; otherwise, compilation errors will occur)
- # Import "C:/program files/common files/system/ADO/msado15.dll" no_namespace Rename ("EOF", "adoeof ")
2. Connection creation and initialization
- // Related member variables
- _ Connectionptr m_conn;
- _ Recordsetptr m_res;
-
- // Member function block (generally written in cdocment class constructor)
- Try
- {
- Coinitialize (null );
- M_conn.createinstance (_ uuidof (connection ));
- Cstring strfilename;
- Strfilename = "mybase. mdb"; // Add the file name of your database. The editing status should be in the directory of the source file.
- M_conn-> open ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + strfilename,
- "", "", Adconnectunspecified); // user name, password
- M_res.createinstance (_ uuidof (recordset ));
- }
- Catch (_ com_error e) // exception detection
- {
- Afxmessagebox ("database connection error! ", Mb_iconexclamation );
- Exit (0); // The program exits due to an error.
- }
3. Database-related operations (there are many operation methods, and only one simple operation is provided here)
Assume that the database table is designed as follows:
Table Name: mytable
Table Design:
Automatic ID
String type name
Bool type sex
(1) Increase
- _ Variant_t m_resa; // It can be declared as a member variable.
-
- Cstring strmyname = "myname ";
- Cstring strsex = "true ";
-
- Cstring SQL;
- SQL = "insert into mytable (name, sex )";
- SQL + = "values ('" + strmyname + "',";
- SQL + = "" + strsex + "";
- SQL + = ")";
-
- Try
- {
- M_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text); // execute the "add" Operation
- }
- Catch (_ com_error E)
- {
- Afxmessagebox ("database addition error", mb_iconexclamation );
- Exit (0); // The program exits due to an error.
- }
(2) Delete
- _ Variant_t m_resa; // It can be declared as a member variable.
-
- Cstring Strid = "1"; // ID of the record to be deleted
- Cstring SQL;
-
- SQL = "delete from mytable"; // note that spaces are required
- SQL + = "where id =" + Strid; // other syntaxes for detailed query of SQL statements
-
- Try
- {
- M_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text); // execute the "add" Operation
- }
- Catch (_ com_error E)
- {
- Afxmessagebox ("database deletion error", mb_iconexclamation );
- Exit (0); // The program exits due to an error.
- }
(3) Change
- _ Variant_t m_resa; // It can be declared as a member variable.
-
- Cstring SQL;
- Cstring strmyname = "myname ";
- Cstring strsex = "true ";
- Cstring Strid = "1"; // ID of the record to be updated
-
- SQL = "Update mytable set ";
- SQL + = "name = '" + strmyname + "',";
- SQL + = "Sex =" + strsex + "";
- SQL + = "where id =" + Strid;
-
- Try
- {
- M_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text); // execute the "add" Operation
- }
- Catch (_ com_error E)
- {
- Afxmessagebox ("database deletion error", mb_iconexclamation );
- Exit (0); // The program exits due to an error.
- }
(4) Query
- _ Variant_t m_resa; // It can be declared as a member variable.
- Cstring SQL;
- SQL = "select * From mytable"; // the query statement changes, and the following statement also changes.
-
- // # Include <afxtempl. h> // Add the header file.
- // Carray <cmyclass, cmyclass &> m_array; // use a container to save the data you have obtained.
-
- Try
- {
- M_res = m_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text );
- }
- Catch (_ com_error E)
- {
- Afxmessagebox ("database query error", mb_iconexclamation );
- Exit (0 );
- }
-
- // M_array.removeall (); // clear the container
-
- Try
- {
- While (! M_res-> adoeof) // looping records
- {
- _ Variant_t vid, vname, vsex;
- Vid = m_res-> getcollect ("ID ");
- Vname = m_res-> getcollect ("name ");
- Vsex = m_res-> getcollect ("sex ");
- //////////////////////////////////////// ///////
- Int NID;
- Nid = (long) vid. lval;
- //////////////////////////////////////// ///////
- Cstring strname;
- If (vt_null! = Vname. Vt) // if the data is not empty
- {
- Strname = (lpctstr) vname. bstrval;
- }
- //////////////////////////////////////// ///////
- Bool bsex;
- If (vt_null! = Vsex. Vt)
- {
- Bsex = (bool) vsex. boolval;
- }
- //////////////////////////////////////// ////////
- // Cmyclass one (NID, strname, bsex); // create a Data Object
- // M_array.add (one); // Add an array
- M_res-> movenext (); // move to the next record
- }
- }
- Catch (_ com_error E)
- {
- Afxmessagebox ("database query error", mb_iconexclamation );
- Exit (0 );
- }
4. Shut down the database
- Try
- {
- If (m_res! = NULL)
- {
- M_res-> close (); // close the record set
- }
- If (m_conn! = NULL)
- {
- M_conn-> close (); // close the connection
- }
- }
- Catch (_ com_error E)
- {
- Afxmessagebox ("database disconnection error", mb_iconexclamation );
- Exit (0 );
- }
5. Summary
Database Operations are diverse. You can check the relevant information.
Here, we only introduce a simple operation. We can encapsulate the object and directly call it.