Delphi database programming tutorial (3)

zhaozj2021-02-16  53

Section III ADO (ActiveX Data Objects) Programming Model

If the article "introduced into ADO in Delphi", ADO is a set of COM (Component Object Model Component Object Model) Components (DLLs), which allows you to access the database as accessing your email and file system. The application written using ADO does not require BDE. In order to access various databases with ADO, you need to have an ADO / OLE DB (Object Linking and Embedding Object Links and Embedded) libraries. Using ADO may existing in your computer: These files are distributed as part of Windows98 / 2000. If you are using Windows95 or NT, you may need to distribute or install the ADO engine. The CD of the Delphi 5 contains installation files with MDAC-Microsoft Data Access Components (Microsoft Data Access Components). You should be sure to use the latest version of MDAC, which can be obtained from Microsoft's official website. MDAC is a key technique that enables Universal Data Access. They include ActiveX Data Objects (ADO), OLE DB, and OPEN DATABASE CONNECTIVITY (ODBC).

Note: In order to install properly on the Windows95 computer, MDAC requires DCOM95 (Distributed Component Object Model Distributed Component Object Model) has been installed. For the correct registration, the components installed by the MDAC depends on the DLLs installed by the DCOM95. Note that DCOM95 is not required in NT4.0. In some cases, DCOM does not need to be installed in a computer running Windows. But if it is not installed, the DCOM98 should be installed before installing MDAC.

There is no need to discuss OLE DB and ADO, let us transfer to a more practical issue.

ADO objects (ADO Objects)

The ADO programming model is established around several ADO objects, which provide a variety of ways to access various data. These objects provide a function of connecting data sources, query, and updating records, and reporting errors. Delphi passes several VCL components to access these objects package components. Let's take a look at the objects working with ADO:

Connection object, connect to a data source via a connection string. In BDE / DELPHI, the connection object is a combination of database components and session components.

Command object so that we can operate the data source. It depicts a command (also as query or narrative) to handle addition, deletion, query, or update to database data.

Recordset object is the result of the query (query) command. You can think that RECORDSET is a Delphi table (Table) component or query component. Each row returned by RecordSet contains multiple fields (Field) objects.

Several objects existing in the ADO model include: Field object, Parameter object, and error (Error) object - will come back to talk to them in the next chapter.

Section IV Connect to the ADOEXPRESS connection database

Let's first learn how to connect to the Access database before each component of the AdoExpress collection. Of course, of course we will connect to our sample database --aboutdelphi.mdb (translator plus: Database established in the previous chapter). Delphi (5) is mainly supported by AdoExpress components on the component panel ADO page. Several other database enable components will be used in this course. Currently, we will focus on the smallest component set required to access the Access database with ADO.

Run Delphi to create a new application with empty form.

In order to access data of the Access database through ADO and Delphi, you must add at least three Data Aware components in your project. The first is the DBGRID-DBGRID-for browsing from a table or by query. This is followed by DataSource (Data Access Data Access Page), which is used to connect DBGRID components on the dataset and forms to achieve potential data set data, and edit. Finally, ADOTABLE (ADO page), which draws a table obtained from the ADO database. Drag and drop them on the form (FORM), the component names take defaults. At this time, the table should be shown in the figure:

If you are now running the program, there is no data display in the Grid - of course because we don't really connect to the database. Note that another point: only Grid makes it visible, the other two components are invisible controls (Controls-unvisible).

Link Between Components

In order to display data in the database, we must connect three components together. The following settings are made in the object monitor (Object Inspector):

DBGRID1.DATASOURCE = Datasource1; DataSource1.dataset = adotable1;

Now we have a difficult part, in order to get data from the database, we must establish a connection string (Connectionstring). This string indicates the physical storage location and access method of the database. When you double-click the IdOTable component's CONNECTIONSTRING attribute, you will get the following dialog:

When establishing a connection string, we have two options: use the Data Link File (. UDL) or manually establish a connection string. Now let's create it, click the Build button - pop up the Data Link Properties dialog. This dialog has 4 pages. The Provider page allows you to specify the provider - select Microsoft Jet 4.0 Ole DB Provider. Click Next to boot us to enter the second page: Connection. Select the omitted button to browse to find our database (AboutDelphi.mdb). You can click the Test Connection button to verify that you can successfully connect - it should be successful. Other pages are temporarily not set. Finally, click OK to close the Data Link Properties dialog box, then click OK to close the Connectionstring dialog - The connection string is stored in the connectionString property of the ADOTABLE component. The connection string is as follows: provider = microsoft.jet.OleDb.4.0; data source = c: /! Gajba / about / aboutDelphi.mdb; persist security info = false

In order to complete the design, we must set the table name accessed through the Adotable component - once again using Object Inspector.

ADOTABLE1.TABLENAME = Applications

If you want to see the data of the database while design, you can use the ADOTABLE ACTIVE property - set it to true.

what! If you have made all steps above, now you have seen the Applications table only one record (line). When you run an application, you can even change the data of the database. Of course, you can't do more - this is the simplest ADO I can think of.

This part can now end. In the next chapter, we will discuss Delphi to provide all ADO components and how they communicate with the remaining data visualization (Data-Aware components) to create a more powerful Delphi database application.

December 22, 2002 20:14

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

New Post(0)