Use ADO database programming in C ++ Builder5

Source: Internet
Author: User
In C ++ Builder5, use ADO database programming-general Linux technology-Linux programming and kernel information. The following is a detailed description. This article introduces how to use the new ADO control in the C ++ Builder 5 environment, and provides a simple example.

I. ADO Overview

ADO (Active Data Object) is a database model of Microsoft Based on ole db. It implements a series of COM interfaces and implements Data access in a broad sense through Data Provider and Data Consumer. The ADO model simplifies data Access. For example, you can use the Jet ole db Provider to establish a non-DSN connection to the Access database. On the other hand, it is also widely used in Internet applications such as ASP.

Among the many new features in C ++ Builder 5/Delphi 5, the introduction of ADO Data Control is quite attractive, which makes ADO programming as easy as in Visual Basic 6. Because the VCL class library encapsulates ADO more rationally, the old data controls can be easily upgraded to ADO support, unlike some DAO or RDO controls in VB that are not compatible with ADO.

Ii. Use the ADO control

In C ++ Builder 5, the ADO control is essentially a data access component. If you have experienced database development in a previous C ++ Builder version, you will find the data source component (TdataSource) and Data awareness component (such as TDBGrid) connected to the data access component) as before, you only need to connect the TdataSource to the ADO control.

To perform simple table operations or queries, you can use TADOTable or TADOQuery. Generally, you can use the TADODataSet control. The basic attributes are set as follows:

(1) ConnectionString attribute: Click the ellipsis in the attribute column. In the Configuration window displayed, select "Use Connection String" and press the Build button. In the following window, select the data Provider. Take Microsoft's Northwind example Access database as an example. select Microsoft Jet 4.0 ole db Provider and click Next to select the database path and file name. Click the Test Connection button to Test the database Connection. Click "OK ".

(2) CommandText attributes: You can use CommandText Editor to generate SQL statements or Shape statements.

Note: TADODataSet does not support DML statements that do not return result sets, such as DELETE, INSERT, and UPDATE. If you want to use these statements, select TADOCommand or TADOQuery.

These ADO controls can implement database Connection, or set the Connection attribute to a TADOConnection Control name, and set the ConnectionString attribute in the TADOConnection control. All attributes can also be set using code during running.

3. One Master/Detail instance

The following uses a simple example to describe the application of the ADO control. Here we use the Shape statement to implement the Master/Detail relationship. For more information about Shape statements, see the MSDN documentation.

1. Create a new project and place the following controls in the form (the attribute values with ellipsis are not set first ):

Name control type Property Value
ADOConnection1 TADOConnection ConnectionString...
ADODataSet1 TADODataSet Connection ADOConnection1 CommandText...
Performance1 TDataSource DataSet ADODataSet1
DBGrid1 TDBGrid DataSource performance1
ADODataSet2 TADODataSet DataSetField...
Performance2 TDataSource DataSet ADODataSet2
DBGrid2 TDBGrid DataSource performance2

2. Set the ConnectionString value of ADOConnection1

Select MSDataShape as the Data Provider and set the path and name of the Northwind database.

3. Set the CommandText value of ADODataSet1 as follows:

SHAPE {select * from Orders}
APPEND ({select * from [Order Details]} AS Details
RELATE OrderID TO OrderID)

The connection between the primary table Orders and the subtable Order Details through OrderID is defined here.

4. Right-click the ADODataSet1 control and select Fields Editor... -> Right-click and choose Add fields... Or Add all fields. Be sure to select the "Details" item. This is the dataset defined in the Shape statement for connecting sub-tables to the master table.

5. Set

The DataSetField value of ADODataSet2. In this case, the available value is ADODataSet1Details. Selecting this value indicates that it is connected to the Details column of adodataset1.

6. When the Details column appears in DBGrid1, each of its records is of the DataSet type. You can hide the display of the Details column in DBGrid1: select the Columns attribute of DBGrid1, click Add all Fields in the toolbar of the pop-up window, select Details, and click Delete Selected.

7. Run this program. When the data pointer moves in the main table Orders, the subtable Order Details is automatically updated to display detailed entries of different Orders.

From the examples above, C ++ Builder 5 is fully capable of developing complex ADO applications. Its powerful VCL control enables developers to focus on the logic and functional design of databases, you don't have to worry about complicated regular code and operations.
Related Article

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.