Example of accessing a database using the ADO control

Source: Internet
Author: User

The following uses the Microsoft Office Access database as an example to explain how to Access the database through the ADO control. This method is easy to access the database without writing code.
1. Create the database student. mdb in Access and add the table stu_info, for example:

The field name here uses Chinese characters. It is only for the convenience of teaching. We recommend that you use an English field name in your actual work.
2. Create an MFC dialog project AdoCtrl.
3. Add the following two ADO controls in the dialog box Editor:
ADO Data control, used to establish a database connection.
ADO DataGrid Control, used to represent a result record set.
In the dialog box Editor, right-click the menu and Choose Insert ActiveX Control. In the displayed dialog box, select Microsoft ADO Data Control, Version 6.0. --> click OK, in this way, the ADO Data Control IDC_ADODC1 is added to the dialog box;
In the same way, select Microsoft DataGrid Control and add Version 6.0 to the dialog box. In this way, the DataGrid Control IDC_DATAGRID1 is added to the dialog box.

4. Set the properties of the Connection Control in the dialog box.
Set Properties of the ADO Data Control: select the Control page in the Properties dialog box, select the Use Connection String option, click the Build button, and select Microsoft Jet 4.0 OLEDB Provider in the displayed dialog box. Click Next and select the name of the newly created Access database file student. mdb. Point test connection, should be able to connect to the database through. Open the properties settings of the ADO Data control again, select the RecordeSource page, select 2-adCmdTable in Command Type, and select the stu_info Table in Table Or StoredProcedure Name.
Set the properties of the DataGrid Control: In the Properties dialog box, select the Control page, select the Allow AddNew and Allow Delete check boxes, and then select the All page. Set the DataSource parameter to the ID of the Connection Control, that is, IDC_ADODC1.
5. Run the program
You can connect to the database in the dialog box and retrieve the table data and display it in the Grid control. For example:
 
From qq1712088151
 

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.