1. Computing
Send to a Friend via Email
Connecting to a database. BDE? ADO?
Page 4: Connecting to a database with ADOExpress.
 More of this Feature
• Page 1: Delphi database connectivity
• Page 2: the BDE
• Page 3: ADO, ADO Objects

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
Discuss!
  Related Resources
• free DB Course.TOC
• Delphi DB articles
 Elsewhere on the Web
• MS Data Access Page
• UDL Files — Microsoft Data Link Files
 

Before going on to the brief explanation of each component in AdoExpress collection, let's first see how to connect to an Access database. Of course, we will be connecting to our AboutDelphi.mdb sample database.

Delphi (5) ADO support is concentrated in the ADOExpress components on the ADO tab of the component palette. Several other database enabled components will be used through this course. For the moment we will focus on the minimal set of components needed to access an Access database with ADO.

Start Delphi, this will open a new application with one blank form.

In order to be able to access data in an Access database with ADO and Delphi, you must add at least three data aware components to our project. First, the DBGrid on the DataControls component page - used to browse through the records retrieved from a table or by a query. Second, the DataSource (DataAccess Page) used to provide a link between a dataset and DBGrid component on a form that enable display, navigation, and editing of the data underlying the dataset. And finally the ADOTable (ADO page) that represents a table retrieved from an ADO data store. Drop all of them on a form. Let the names be the default one. The form should look something like:

If you run the application now, nothing is displayed in a Grid - of course, we did nothing to really connect to a database. Note just one more thing: only the Grid is displayed, the rest two component are controls - unvisible to the user.

Link between components
In order to display some data from a database we have to link all three components together. Using the Object Inspector, set the following:

DBGrid1.DataSource = DataSource1
DataSource1.DataSet = ADOTable1

We have now reached the hard part, to really get the data from our database we have to build a ConnectionString. This string indicates where the database is physically stored and how we are accessing it. When you double click the ellipsis button at the ConnectionString property of the AdoTable component you get the next dialog box:

Connection String

When building a connection string we have two choices: use the Data Link File (.UDL) or build a connection string by hand. Let's build it. Press the Build button - this pops up the Data Link Properties dialog. This dialog has 4 pages. The Provider tab allows you to specify the provider - select the Microsoft Jet 4.0 OLE DB Provider. The Next button leads us to the second page: Connection. Select the ellipsis button to browse for our database (AboutDelphi.mdb). Press the Test Connection button to see if the connection is successful - it should be. Leave all the other pages as they are. Finally, click on OK to close the Data Link Properties dialog, again OK to close the ConnectionString dialog - the connection string is stored in the ConnectionString property of the ADTTable component. The connection string should look something like:

Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\!gajba\About\aboutdelphi.mdb;
Persist Security Info=False

To finish, we have to set the table name that is to be accessed by the ADOTable component - again use the Object Inspector.

ADOTable1.TableName = Applications

If you want to see the data at design time use the ADOTable Active property - set it to True.

Ha! If you have done all the steps you now see the only one record (row) in the Applications table. When you start the application you can even change the data in the database. Of course, you cannot do much more - this is the simplest ADO example I could think of.

This concludes this chapter. In the next chapter we will address all the ADO component provided with Delphi and how they communicate with the rest data-aware components to crate a powerfull Delphi database application.

DB Course Next Chapter >>
>> Chapter 3: Pictures inside a database

©2014 About.com. All rights reserved.