First Steps to Use FireDAC

From RAD Studio
Jump to: navigation, search

Go Up to Getting Started (FireDAC)


This article guides you through the creation of your first application built using the Delphi edition of FireDAC.

Introduction

This tutorial has three main sections:

  • Establishing the connection to the database: how to use Delphi to create an application that will connect to a database.
  • Selecting rows from the database: hook the data up to a grid and display it at design time.
  • Preparing the application for run time: describes the necessary steps to make an application run as a standalone executable (run time).

The screenshots and instructions below relate to RAD Studio 2007, so there might be minor changes if you use a different RAD Studio release.

Also see the Ron Grove movie: First Steps to Use FireDAC.

Establishing the Connection to the Database

In this article, we use the Microsoft SQL Server's Northwind demo database and a predefined connection definition - MSSQL_Demo. Let's start by creating a new "VCL Forms Application Delphi for Win32".

First, drop a TFDConnection component onto the form selected from the "FireDAC" page of the RAD Studio Tool Palette. This component is responsible for establishing and controlling the database connection.

Next, select MSSQL_Demo from the dropdown list of its ConnectionDefName property. This associates the connection component with the specified connection definition. By using the predefined definitions, you do not need to enter any additional parameters (such as the server name or the default database).

After you set the Connected property to True, FireDAC displays a Login Dialog:

FireDACLoginMSSQL.png

Here you can enter your user credentials. By default, you have three attempts to enter valid credentials. If they all fail, the login process also fails and you will get an error message.

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 should still be set to True, otherwise it will be reset to False and FireDAC will display an appropriate error message.

Selecting Rows from the Database

Now drop a TFDQuery component from the "FireDAC" 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 FDConnection1 to hook the query to a database connection.

Note: If a query component is dropped on a form or a datamodule that already contains one or more TFDConnections, FireDAC automatically sets 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

Press the OK button to close the editor. This stores the SQL command text into the TFDQuery component's SQL property. Next, drop a standard Delphi TDataSource component from the "Data Access" palette page onto your form. Set its DataSet property to FDQuery1. Now drop a TDBGrid control onto the form from the "Data Controls" page and set its DataSource property to DataSource1.

Finally, set FDQuery1's Active property to True. This sends the SQL command to the DBMS, which executes the command and returns a result set. This data is displayed by the DBGrid1 control:

FDQuerySetActive.png

Preparing the Application for Run Time

To allow your application to work at run time you need to:

Now your application is ready to run. These components assure that the necessary units get linked into your application executable. For real world applications, these components are typically dropped on a main data module.

Summary

This article has provided a tutorial showing how to create a simple client-server application using FireDAC for Delphi. It shows how to use the FireDAC 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 on how to setup the connection definitions. For other DBMS Getting Started demo applications, see the FireDAC\Samples\Getting Started folder.

See Also