VC + + Package ADO class and how to use

Source: Internet
Author: User
Tags knowledge base

Operating system: Windows 7
Software Environment: Visual Studio 2008, Microsoft SQL 2005
Objective: To introduce an already encapsulated ADO class that simply explains how to import and use

First of all, this package of ADO class is downloaded in the "VC Knowledge Base", because recently in the database course design, so the VC under the use of ADO connection database is not very understanding, so if this article has the wrong place, please do not hesitate to point out. Specific ADO class Each package function, please go to "original address" view. Only a primer on using this ADO wrapper class is described here, such as connecting to a database, displaying records, and so on. A test example is as follows:

Steps to use:
1. First download the encapsulated ADO class from "Here", you can use Notepad and so on to open the inside of the file, for each function has commented pretty detailed;
2. Prepare the database, can be SQL 2000 or higher, here I am using SQL 2005, create a new vc_sqlexample database A student table, the format is as follows:

3. Specific table content can be casually written, and then create a new MFC based on the dialog box project (named Test 1), right-click the project name, add the encapsulated ADO class into the project;
4. Initialize the COM environment and add the following code:

[CPP]View PlainCopy
  1. /* Test 1.h */
  2. Class C Test 1App: public CWinApp
  3. {
  4. Public :
  5. virtual int exitinstance ();
  6. }
  7. /* Test 1.cpp */
  8. BOOL C Test 1app::initinstance ()
  9. {
  10. CoInitialize (NULL);
  11. }
  12. int c Test App::exitinstance ()
  13. {
  14. CoUninitialize ();
  15. return CWinApp::ExitInstance ();
  16. }


5. Add "Ado.h" in the dialog (Project based dialog) header file to add the object to the class declaration. and the OnInitDialog function inside the implementation file connects to the database.

[CPP]View PlainCopy
  1. #include "ado.h"
  2. Class C Test 1DLG: public CDialog
  3. {
  4. Protected
  5. Cadoconnection m_adoconnection;
  6. Cadorecordset M_adorecordset;
  7. }
  8. /* Test 1Dlg.cpp: Implement file */
  9. BOOL C Test 1dlg::oninitdialog ()
  10. {
  11. if (M_adoconnection.connectsqlserver ("127.0.0.1","Vc_sqlexample","sa","wjs1314"))
  12. {
  13. M_adorecordset.setadoconnection (&m_adoconnection);
  14. M_adorecordset.setcursorlocation ();
  15. M_adorecordset.open ("student", adCmdTable);
  16. M_adoconnection.begintrans ();
  17. UpdateRecord (TRUE);
  18. }
  19. Else
  20. {
  21. MessageBox ("Database connection failed!");
  22. }
  23. }


6. Add a variable to the control (right-click the control to add a variable), a function to display the data, and a button function for the previous record and the next record

[CPP]View PlainCopy
  1. Class C Test 1DLG: public CDialog
  2. {
  3. Public
  4. COleDateTime M_strbirth;
  5. CString M_strbrief;
  6. CString M_strstuid;
  7. CString M_strname;
  8. CString M_strsex;
  9. bool UpdateRecord (bool bload = TRUE);
  10. }
  11. BOOL C Test 1dlg::updaterecord (bool bload)
  12. {
  13. if (M_adorecordset.getrecordcount () < 1) return FALSE;
  14. if (bload)
  15. {
  16. if (!m_adorecordset.getcollect ("Stuid", M_strstuid)
  17. || !m_adorecordset.getcollect ("Stuname", M_strname)
  18. || !m_adorecordset.getcollect ("Stusex", M_strsex)
  19. || !m_adorecordset.getcollect ("Birth", M_strbirth)
  20. || !m_adorecordset.getcollect ("brief", M_strbrief))
  21. {}
  22. UpdateData (FALSE);
  23. return TRUE;
  24. }
  25. }
  26. void C Test 1dlg::onbnclickedbutton1 ()
  27. {
  28. //TODO: Add control notification handler code here
  29. UpdateData ();
  30. if (m_adorecordset.moveprevious ())
  31. UpdateRecord ();
  32. }
  33. void C Test 1dlg::onbnclickedbutton2 ()
  34. {
  35. //TODO: Add control notification handler code here
  36. UpdateData ();
  37. if (M_adorecordset.movenext ())
  38. UpdateRecord ();
  39. }


7. Start the database and run the compilation program.

http://blog.csdn.net/akof1314/article/details/4657780

VC + + Package ADO class and how to use

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.