Using ADO Databinding in Visual C++
Using ADO databinding in Visual C++ requires the following steps.
- Add an ADO Data Control.
- Point to a data source.
- Specify the record source (SQL query or data retrieval language).
- Add an ADO data-bound control.
- Connect the data-bound control to an ADO Data Control.
- Select the fields to bind to the ADO Data Control's record source.
To use ADO databinding in Visual C++
- Create an MFC Dialog application or MFC Formview application using the MFC AppWizard.
- Add the Microsoft ADO Data Control to the dialog box; see Inserting the Control into a Visual C++ Application.
- Point the ADO Data Control to your OLE DB data source.
- Right-click on the ADO Data Control and select Properties from the shortcut menu.
- On the Control tab, select the Use Connection String. You can use the supplied provider or you can delete it.
- Select Build. If you deleted the provider from Use Connection String, you will now be able to define one. After you define the provider, access the properties of the ADO Data Control again and select Build again to continue.
If a provider is defined in Use Connection String before you select Build, you will now be able to define the data link properties. This displays the DataLink Wizard.
- Change the Provider if necessary, and define Location and Data Source values, as appropriate for your provider. For example, if you are using a SQL Server provider, Location specifies the database server and Data Source specifies the database. If you are using an ODBC provider, the Data Source corresponds to the ODBC DSN.
- Select the Authentication tab and set values for User Name and Password, if required by the data source.
- Return to the Connection tab and click Test Connection to test the data source. Scroll to the end of the Results window to see if the test passed. If it failed, check the configuration of your data source. Common errors include invalid passwords and incorrect values for the Location and Data Source fields.
- Exit the DataLink Wizard and return to the property sheet for the ADO Data Control.
- In the RecordSource tab, enter a query into the Command Text (SQL). The data-bound controls can bind to the results of this query. The query will usually be SQL. However, some OLE DB providers do not use SQL.
- Set any other ADO Data Control properties as needed and close the property sheet for the ADO Data Control.
- Add a data-bound control. For example, add the DataGrid control. (Do not confuse the DataGrid control with the RDO DBGrid control.)
- Set the DataGrid's properties.
- Right-click on the DataGrid and select Properties from the shortcut menu.
- Select the All tab and set the DataSource property to the ADO Data Control. Click on the DataSource drop-down list and find the ID of the ADO Data Control. The default ID name is IDC_ADODC1.
- To run in test mode, use CTRL+T. You will be able to scroll through the data. Use the Esc key or close the dialog box to end test mode.
If you compile and run the program, you will be able to scroll through the data as well.
Back to Databinding with ActiveX Controls in Visual C++.