Section 3 ADO ( ActiveX Data Objects) Programming Model
As described in the article "Introducing ADO in Delphi", ADO is a set of COM (Component Object Model) components (DLLs) that allow you to access databases like email and file systems. Applications written using ADO do not require BDE. In order to use ADO to access various databases, you need the ADO/OLE DB (Object Linking and Embedding) library. What you need to use ADO may already exist on your computer: these files are distributed by Microsoft as part of Windows 98/2000. If you are using Windows 95 or NT, you may need to distribute or install the ADO engine. The Delphi 5 CD contains the installation files for MDAC-Microsoft Data Access Components. You should make sure you are using the latest version of MDAC, which is available from Microsoft's official website. MDAC is the key technology that makes Universal Data Access work. They include ActiveX Data Objects (ADO), OLE DB and Open Database Connectivity (ODBC).
Note: In order to install correctly on a Windows 95 computer, MDAC requires DCOM95 (Distributed Component Object Model) to be installed. For proper registration, the components installed by MDAC depend on the DLLs installed by DCOM95. Note that DCOM95 is not required in NT4.0. In some cases, DCOM does not need to be installed in computers running Windows. But if it is not installed, DCOM98 should be installed before installing MDAC.
There is no need to discuss OLE DB and ADO too much, let us move to more practical issues.
ADO Objects
The ADO programming model is built around several ADO objects that provide multiple methods for accessing various data objects. These objects provide the functionality to connect to data sources, query and update recordsets, and report errors. Delphi accesses these objects through encapsulated components through several VCL components. Let's take a look at the objects that work with ADO:
A Connection object that connects to a data source through a connection string. In BDE/Delphi, the connection object is a combination of database components and session components.
Command (Command) object allows us to operate the data source. It describes a command (also considered a query or statement) that can handle adding, deleting, querying, or updating database data.
Recordset (record set) object is the result of the query (Query) command. You can think of Recordset as a Delphi table (Table) component or query (Query) component. Each row returned by the Recordset contains multiple Field objects.
Several other objects that exist in the ADO model are: Field objects, Parameter objects, and Error objects—we will come back to them in the following chapters.
Section 4 Use ADOExPRess to connect to the database
Before giving a brief explanation of each component of the ADOExpress collection, let us first understand how to connect to an Access database. Of course, of course we will connect to our sample database - AboutDelphi.mdb (Translator's addition: the database created in the previous chapter).
Delphi(5) mainly supports ADO through the ADOExpress component on the ADO page of the component panel. Several other database-enabling components will be used in this course. For now, we'll focus on the minimal set of components required to access an Access database with ADO.
Run Delphi and create a new application with an empty form.
In order to access Access database data through ADO and Delphi, you must add at least three data aware components to your project. The first is the DBGrid of the DataControls component page—used to browse records obtained from a table or through a query. The second is DataSource (DataAccess data access page), which is used to connect the data set and the DBGrid component on the form, thereby realizing the realization, navigation and editing of potential data set data. Finally, there is ADOTable (ADO page), which depicts a table obtained from the ADO database. Drag and drop them all on the form (Form), and the component names will take the default values. The form should now look like this:
If you run the program now, no data will be displayed in the Grid - of course, because we are not actually connected to the database. Note another point: only the Grid is visible, the other two components are controls-unvisible.
Link between components
In order to display data from the database, we must connect three components together. Make the following settings in the Object Inspector:
DBGrid1.DataSource = DataSource1;
DataSource1.DataSet = ADOTable1;
Now we get to the hard part, in order to actually get the data from the database, we have to create a ConnectionString. This string indicates the physical storage location of the database and how to access it. When you double-click the ellipsis button of the ConnectionString property of the AdoTable component, you will get the following dialog box:
Data Source=C:/!gajba/About/aboutdelphi.mdb;
Persist Security Info=False
To complete the design, we must set the table name accessed through the ADOTable component—again using the Object Inspector. ADOTable1.TableName = applications If you want to see database data at design time, use the ADOTable's Active property—set it to True. ha! If you have followed all the steps above, you will now see that the Applications table has only one record (row). When you run the application, you can even change the data in the database. Of course, you can't do more than that - this is the simplest example of ADO I can think of. This section can now be concluded. In the next chapter, we will discuss all the ADO components provided by Delphi and how they communicate with the remaining data-aware components to create a more powerful Delphi database application. December 22, 2002 20:14