Programming with ADO database in CB5

Source: Internet
Author: User
Tags new features ole access database

This paper introduces the application of the new ADO control in C++builder 5 environment, and gives a simple example.

An overview of ADO

ADO (Active Data Object) is a Microsoft Database model based on OLE DB. It implements a series of COM interfaces, and realizes the generalized data access through data provider (Provider) and data Consumer. The ADO model simplifies data access on the one hand, such as using Jet OLE DB provider to implement DSN-free connections to access databases, and on the other hand, it is widely used in Internet applications such as ASP.

Among the many new features in C++builder 5/delphi 5, the introduction of ADO data controls is compelling, making ADO programming as easy as in Visual Basic 6. Since the VCL class library is more reasonable for ADO encapsulation, old data controls can easily be upgraded to ADO support, unlike some DAO or RDO controls in VB that cannot be compatible with ADO.

Second, the use of ADO control

In C++builder 5, the ADO control is essentially a data access component. If the reader has c++builder previous versions of database development experience, you will find that the data source components (Tdatasource) and data-aware components (such as Tdbgrid) that are connected to the data access component are used as before, just the Tdatasource You can connect to the ADO control.

If you want to implement a simple table operation or query, you can use Tadotable or tadoquery. More generally, you can use the Tadodataset control, whose basic properties are set in the following ways:

(1) ConnectionString property: Click the ellipsis in the property bar, the Configuration window appears, select Use Connection String, press the Build button. Select the data provider in the next window, take Microsoft's Northwind sample Access database as an example, choose Microsoft Jet 4.0 OLE DB Provider, press the Next button, and select the path and file name of the database. Click the Test Connection button to test the database connection. Click "OK".

(2) CommandText property: You can use CommandText editor to generate SQL statements or shape statements.

Note that Tadodataset does not support DML statements that do not return a result set, such as Delete,insert,update. If you want to use these statements, select Tadocommand or Tadoquery.

These ADO controls can already implement a database connection, or you can set their connection property to a Tadoconnection control name, and set the ConnectionString property in the Tadoconnection control. All properties can also be set in code during run time.

三、一个 Master/detail Example

The following is a simple example to illustrate the application of the ADO control. Here we use the SHAPE statement to implement the Master/detail relationship. Please refer to the MSDN documentation for a detailed explanation of the shape statement.

1. Create a new item and place the following controls on the form (the property value with ellipses is not set first):

Name control Type Property property value

ADOConnection1 tadoconnection ConnectionString ...

ADODataSet1 tadodataset Connection ADOConnection1 CommandText ...

DataSource1 Tdatasource DataSet ADODataSet1

DBGrid1 TDBGrid DataSource DataSource1

ADODataSet2 tadodataset Datasetfield ...

DataSource2 Tdatasource DataSet ADODataSet2

DBGrid2 TDBGrid DataSource DataSource2

2. Set the ConnectionString value of the ADOConnection1

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

3. The CommandText value for setting ADODataSet1 is as follows:

SHAPE {SELECT * from Orders}

APPEND ({select * from [Order Details]} as Details

RELATE OrderID to OrderID)

Here defines the main table orders and the child table Order details through the OrderID connection.

4. Right-click on the ADODataSet1 control, select fields editor...-> right click-> Select Add Fields ... Or add all fields, be sure to select the "Details" item, which is the dataset we defined in the SHAPE statement for the child table Connection master table.

5. Set

The Datasetfield value of the ADODataSet2, at which point the selectable value is adodataset1details, and it is selected to indicate that it is connected to the details column of ADODataSet1.

6. The Details column appearing in the DBGRID1, each of which is a dataset type. You can hide the display of the Details column in DBGRID1: Select the DBGrid1 Columns property, click Add All on the toolbar of the pop-up window, select Details, and click Delete Selected.

7. Run the program, and when the data pointer moves through the main table orders, the child table Order details are automatically updated to show the detail entries for the different orders.

From the above example, C++builder 5 is fully equipped to develop complex ADO applications, and its powerful VCL controls enable developers to focus on the logical and functional design of the database without having to worry about cumbersome routine code and operations.

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.