This article guides you through the creation of your first application built using the Delphi edition of AnyDAC.
This tutorial has three main sections:
The screen shots and instructions below relate to Delphi 2007, so there will be minor changes if you use a different Delphi release. And the Ron Grove movie:
First, drop a TADConnection component onto the form selected from the "AnyDAC" page of the Delphi Tool Palette. This component is responsible to establish and control the database connection.
Next, select MSSQL_Demo from the dropdown list of its ConnectionDefName property. This will associate the connection component with the specified connection definition. By using the predefined definitions you do not need to enter any additional parameters (e.g. the server name or the default database).
After setting the Connected property to True, AnyDAC will display a Login Dialog:
Here you can enter your user credentials. Press the OK button to establish the connection to the DB and to create a user session on the DBMS if this DBMS supports this feature.
After the connection is successfully established, the Connected property will still be set to True, otherwise it will be reset to False and AnyDAC will display an appropriate error message. By default, you do have three attempts to enter valid credentials. If they all fail, the login process also fails and you will get an error message.
Now drop a TADQuery component from the "AnyDAC" palette page onto the form. This component is responsible for the execution of SQL commands, fetching rows from the DB and for posting changed data back to the DB.
Set its Connection property to ADConnection1 to hook the query to a database connection.
Note: If a query component is dropped on a form or datamodule which already contains one or more TADConnections, AnyDAC will automatically set the query's Connection property to point to the connection that was created first.
Click on its SQL property and enter the following SQL command into the editor window:
SELECT * FROM Orders
Next, drop a standard Delphi TDataSource component from the "Data Access" palette page onto your form. Set its DataSet property to ADQuery1. Now drop a TDBGrid control onto the form from the "Data Controls" page and set its DataSource property to DataSource1.
Finally, set ADQuery1's Active property to True. This will send the SQL command to the DBMS, which will execute the command and return a result set. This data will be displayed by the DBGrid1 control:
To allow your application to work at runtime you will need:
Now your application is ready to run. These components assure that the necessary units get linked into your application execute. For real world applications this components are normally dropped on a main data module.
This article has provided a tutorial showing how to create a simple client-server application using AnyDAC for Delphi. It shows how to use the AnyDAC connection and query components to establish a connection to the DB and return rows to the client without actually writing any code.
We suggest that you also read the Setting up Connections article for all the details how to setup the connection definitions.
For other DBMS Getting Started demo applications see AnyDAC\Samples\Getting Started folder.
What do you think about this topic? Send feedback!