Use ADO database programming in C ++ Builder 5

zhaozj2021-02-08  216

Use ADO database programming in C Builder 5

--- Reprinted from "Computer World Daily" (text / rash)

---- This article introduces the use of new ADO controls in the C Builder 5 environment, and gives a simple instance.

---- 1, ADO Overview

---- ADO (Active Data Object) is Microsoft's OLE DB based database model. It implements a range of COM interfaces that implement broad data access through data providers and data consumes (DATA Consuer). On the one hand, the ADO model simplifies data access, for example using Jet OLE DB Provider to implement a DSN connection of the Access database; on the other hand, it has also been widely used in Internet applications such as ASP.

---- In C Builder 5 / Delphi 5, the introduction of ADO data controls is more compelling, which makes ADO programming as it is as easy as Visual Basic 6. Since the VCL class library is more reasonable for ADO packages, older data controls can be easily upgraded to ADO support, not some DAO or RDO controls in VB cannot be compatible with ADO.

---- Second, use ADO controls

---- In C Builder 5, the ADO control is essentially a data access component. If the reader has the previous version of the C Builder's previous version of the database development, it will find that the use of data source components (such as TDBGRID) connected to the data access component is the same as before, just connect TDataSource. On the ADO control.

---- If you want to implement a simple table operation or query, you can use Tadotable or TadoQuery. More generally, the TadodataSet control can be used, and the basic attributes are equipped with as follows:

---- (1) Connectionstring Attribute: Click the omit number in the properties bar, the configuration window appears, select "Use connection string", and press the build button. Select the data provider in the next window, take Microsoft's Northwind Example Access database as an example, select Microsoft Jet 4.0 OLE DB Provider, press the Next button, select the path and file name of the database. Click the Test Connection button to test the database connection. Click "OK".

---- (2) CommandText Properties: You can generate SQL statements or Shape statements with CommandText Editor.

---- Note that tadodataset does not support DML statements that do not return the result set, such as delete, insert, update. If you want to use these statements, select Tadocommand or TadoQuery.

---- These ADO controls themselves can implement database connections, or set their connection attribute as a Tadoconnection control name, and set the Connectionstring property in the Tadoconnection control. All attributes can also be set to run during operation.

---- Third, an Master / Detail instance

---- The application of the ADO control is illustrated by a simple example below. Here we use the Shape statement to implement Master / Detail relationships. For detailed explanation of the Shape statement, please refer to the MSDN documentation.

---- 1. Create a new project, and place the following controls on the form (ellipsis property value to not set): Name Control Type Attribute Value ADOConnection1TADOConnectionConnectionString ... ADODataSet1TADODataSet ConnectionADOConnection1CommandText ... DataSource1TDataSourceDataSetADODataSet1DBGrid1TDBGridDataSourceDataSource1ADODataSet2TADODataSet DataSetField ... DataSource2TDataSourceDataSetADODataSet2DBGrid2TDBGridDataSourceDataSource2

---- 2. Set the connectionstring value of Adoconnection1

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

---- 3. Set the commandText value of Adodataset1 as follows:

Shape {selection * from Orders}

Append ({Select * from [Order Details]} as details

Relate Orderid to Orderid)

---- Here, the primary table orders and sub-table ORDER DETAILs are connected through the ORDERID.

---- 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 defined in the shape statement The sub-table is connected to the primary table.

---- 5. Set

---- Adodataset2's DatasetField value, at this time, the value is available for Adodataset1Details, select it, indicating that the Details column of AdoDataSet1 is indicated.

---- 6. The Details column in DBGRID1 appears, each of which is a DataSet type. You can hide the display of the Details column in DBGRID1: Select the COLUMNS property of DBGRID1, click Add All Fields on the toolbar of the pop-up window, select Details, click Delete SELECTED.

---- 7. Run the program, when the data pointer moves in the primary table ORDERS, the subtoms ORDER DETAILS automatically updates a clear entry for different orders.

---- From the above example, C Builder 5 fully has the ability to develop complex ADO applications, and its powerful VCL control makes developers to focus on database logic and function design, without having to care General code and operation.

转载请注明原文地址:https://www.9cbs.com/read-3236.html

New Post(0)