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