FireDAC.TFDQuery.Async Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample shows you how to configure the ResourceOptions.CmdExecMode property to control the asynchronous execution mode using FireDAC.

Location

You can find the Async sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDQuery\ExecSQL\Async
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

The Async sample demonstrates how to configure the ResourceOptions.CmdExecMode property to configure the different SQL command execution modes. To this end, the sample implements three buttons that execute three SQL statements with different execution modes: amBlocking, amCancelDialog, and amAsync.

Note: See CmdExecMode to notice the difference between the execution modes.

How to Use the Sample

  1. Navigate to the location given above and open Async.dproj.
  2. Press F9 or choose Run > Run.

Files

File in Delphi Contains

Async.dproj
Async.dpr

The project itself.

fAsync.pas
fAsync.fmx

The main form.

Implementation

Before running the sample, the main components are configured at design time using the Object Inspector as follows:

  • A TFDQuery object named qryExecSQL. This component is used to implement a dataset capable of executing SQL queries. The Connection property is configured to specify the FireDAC connection object that is used to connect to a DBMS.
  • A TDataSource object named DataSource1. This component provides an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the dataset and the grid where the dataset is displayed. To this end, the following properties are set:
  • The DataSet property of DataSource is set to qryExecSQL.
  • The DataSource property of DBGrid1 is set to DataSource1.

When you run the application, click on the Use Connection Definition combo box and select an option to define a connection. When you select an item of the combo box, the sample uses the Open method of qryProducts to execute the SQL statement that retrieves the datasets from the database. Then, the sample displays the data from the datasets using a TDBGrid component. Once the DBGrid1 is filled, you can interact with the sample by clicking on the different buttons. Each button implements a on click event to do the following:

  • The Exec sql comand button. This button implements an event that:
    1. Sets the SQL command to 'delete from {id Orders} where OrderID > 1000000'.
    2. Calls the ExecSQL method to execute the SQL query.
      Note: The default execution mode is amBlocking.
  • The Execute/Open with CancelDialog button. This button implements an event that:
    1. Sets the ResourceOptions.CmdExecMode property of qryExecSQL to amCancelDialog.
      Note: With this execution mode, FireDAC shows a dialog with a Cancel button that allows to cancel the action.
    2. Sets the SQL command to 'select count(*) from {id Order Details} a, {id Order Details} b group by a.OrderID'.
    3. Calls the Open method to execute the SQL statement.
  • The Async executing/opening button. This button implements an event that:
    1. Sets the ResourceOptions.CmdExecMode property of qryExecSQL to amAsync.
      Note: With the amAsync execution mode, the application can get a notification about the termination of the action by using the appropriate event handler or callback. This sample uses an AfterOpen event.
    2. Sets the SQL command to 'select count(*) from {id Order Details} a, {id Order Details} b group by a.OrderID'.
    3. Calls the Open method to execute theSQL statement.

Uses

See Also

Samples