Use ADO in VC)

Source: Internet
Author: User
(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

  1. // Add the last part in stdafx. H (Note: Make sure it is in the last part; otherwise, compilation errors will occur)
  2. # Import "C:/program files/common files/system/ADO/msado15.dll" no_namespace Rename ("EOF", "adoeof ")

2. Connection creation and initialization

  1. // Related member variables
  2. _ Connectionptr m_conn;
  3. _ Recordsetptr m_res;
  4.  
  5. // Member function block (generally written in cdocment class constructor)
  6. Try
  7. {
  8. Coinitialize (null );
  9. M_conn.createinstance (_ uuidof (connection ));
  10. Cstring strfilename;
  11. Strfilename = "mybase. mdb"; // Add the file name of your database. The editing status should be in the directory of the source file.
  12. M_conn-> open ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + strfilename,
  13. "", "", Adconnectunspecified); // user name, password
  14. M_res.createinstance (_ uuidof (recordset ));
  15. }
  16. Catch (_ com_error e) // exception detection
  17. {
  18. Afxmessagebox ("database connection error! ", Mb_iconexclamation );
  19. Exit (0); // The program exits due to an error.
  20. }

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

  1. _ Variant_t m_resa; // It can be declared as a member variable.
  2.  
  3. Cstring strmyname = "myname ";
  4. Cstring strsex = "true ";
  5.  
  6. Cstring SQL;
  7. SQL = "insert into mytable (name, sex )";
  8. SQL + = "values ('" + strmyname + "',";
  9. SQL + = "" + strsex + "";
  10. SQL + = ")";
  11.  
  12. Try
  13. {
  14. M_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text); // execute the "add" Operation
  15. }
  16. Catch (_ com_error E)
  17. {
  18. Afxmessagebox ("database addition error", mb_iconexclamation );
  19. Exit (0); // The program exits due to an error.
  20. }

(2) Delete

  1. _ Variant_t m_resa; // It can be declared as a member variable.
  2.  
  3. Cstring Strid = "1"; // ID of the record to be deleted
  4. Cstring SQL;
  5.  
  6. SQL = "delete from mytable"; // note that spaces are required
  7. SQL + = "where id =" + Strid; // other syntaxes for detailed query of SQL statements
  8.  
  9. Try
  10. {
  11. M_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text); // execute the "add" Operation
  12. }
  13. Catch (_ com_error E)
  14. {
  15. Afxmessagebox ("database deletion error", mb_iconexclamation );
  16. Exit (0); // The program exits due to an error.
  17. }

(3) Change

  1. _ Variant_t m_resa; // It can be declared as a member variable.
  2.  
  3. Cstring SQL;
  4. Cstring strmyname = "myname ";
  5. Cstring strsex = "true ";
  6. Cstring Strid = "1"; // ID of the record to be updated
  7.  
  8. SQL = "Update mytable set ";
  9. SQL + = "name = '" + strmyname + "',";
  10. SQL + = "Sex =" + strsex + "";
  11. SQL + = "where id =" + Strid;
  12.  
  13. Try
  14. {
  15. M_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text); // execute the "add" Operation
  16. }
  17. Catch (_ com_error E)
  18. {
  19. Afxmessagebox ("database deletion error", mb_iconexclamation );
  20. Exit (0); // The program exits due to an error.
  21. }

(4) Query

  1. _ Variant_t m_resa; // It can be declared as a member variable.
  2. Cstring SQL;
  3. SQL = "select * From mytable"; // the query statement changes, and the following statement also changes.
  4.  
  5. // # Include <afxtempl. h> // Add the header file.
  6. // Carray <cmyclass, cmyclass &> m_array; // use a container to save the data you have obtained.
  7.  
  8. Try
  9. {
  10. M_res = m_conn-> execute (_ bstr_t) SQL, & m_resa, ad1_text );
  11. }
  12. Catch (_ com_error E)
  13. {
  14. Afxmessagebox ("database query error", mb_iconexclamation );
  15. Exit (0 );
  16. }
  17.  
  18. // M_array.removeall (); // clear the container
  19.  
  20. Try
  21. {
  22. While (! M_res-> adoeof) // looping records
  23. {
  24. _ Variant_t vid, vname, vsex;
  25. Vid = m_res-> getcollect ("ID ");
  26. Vname = m_res-> getcollect ("name ");
  27. Vsex = m_res-> getcollect ("sex ");
  28. //////////////////////////////////////// ///////
  29. Int NID;
  30. Nid = (long) vid. lval;
  31. //////////////////////////////////////// ///////
  32. Cstring strname;
  33. If (vt_null! = Vname. Vt) // if the data is not empty
  34. {
  35. Strname = (lpctstr) vname. bstrval;
  36. }
  37. //////////////////////////////////////// ///////
  38. Bool bsex;
  39. If (vt_null! = Vsex. Vt)
  40. {
  41. Bsex = (bool) vsex. boolval;
  42. }
  43. //////////////////////////////////////// ////////
  44. // Cmyclass one (NID, strname, bsex); // create a Data Object
  45. // M_array.add (one); // Add an array
  46. M_res-> movenext (); // move to the next record
  47. }
  48. }
  49. Catch (_ com_error E)
  50. {
  51. Afxmessagebox ("database query error", mb_iconexclamation );
  52. Exit (0 );
  53. }

4. Shut down the database

  1. Try
  2. {
  3. If (m_res! = NULL)
  4. {
  5. M_res-> close (); // close the record set
  6. }
  7. If (m_conn! = NULL)
  8. {
  9. M_conn-> close (); // close the connection
  10. }
  11. }
  12. Catch (_ com_error E)
  13. {
  14. Afxmessagebox ("database disconnection error", mb_iconexclamation );
  15. Exit (0 );
  16. }

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.