Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDatabaseDelphiRAD Studio

Learn How to Asynchronously Execute SQL In Delphi With FireDAC

The Async sample shows you how to use the IFDPhysCommand interface to configure the different asynchronous execution modes. To this end, the sample uses a group of four radio buttons. Each radio button sets the Options.ResourceOptions.CmdExecMode property of the IFDPhysCommand interface to one of the following execution modes: amBlockingamNonBlockingamCancelDialog, and amAsync. Then, the sample executes the SQL statement using the selected asynchronous execution mode.Note: To see the differences between the execution modes, click CmdExecMode

Location

You can find the Async sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object PascalDatabaseFireDACSamplesPhys LayerIFDPhysCommandAsync
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

How to Use the Sample

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

Files

File in DelphiContains
IFDPhys_Async.dproj
IFDPhys_Async.dpr
The project itself.
fAsync.pas
fAsync.fmx
The main form.

Implementation

When you run the application, you can interact with the sample using the following objects:

  • TComboBox object labeled as Use Connection Definition.Click the Use Connection Definition combo box and select an option in order to define a connection to a database. The menu shows all the persistent connections defined on the file C:UsersPublicDocumentsEmbarcaderoStudioFireDACFDConnectionDefs.ini. When the connection is defined, the sample uses the CreateCommand method of IFDPhysConnection to create a command interface. Then, the sample enables the Execute button. If you click the Execute button, the sample uses this command interface to execute some SQL commands at run time.
  • TRadioGroup object labeled as Async execution mode.The sample implements a group of radio buttons in order to select the asynchronous execution mode. The options are the following:
    • Select Blocking to set the CmdExecMode property to amBlocking.Note: This is the default execution mode. It blocks the GUI until the action is finished.
    • Select Non Blocking to set the CmdExecMode property to amNonBlocking.Note: The application continues to process GUI messages, but it ignores mouse and keyboard events.
    • Select Cancel Dialog to set the CmdExecMode property to amCancelDialog.Note: With this execution mode, FireDAC shows a dialog with a Cancel button that allows to cancel the action.
    • Select Async to set the CmdExecMode property 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.
  • TEdit object labeled as Timeout.The sample uses the value of the edit component to set the CmdExecTimeout property. The CmdExecTimeout property controls the execution timeout of the database action.
  • TButton object labeled as Execute.When you click the Execute button, the sample implements the following command interface related features:
    1. First, the sample uses the Prepare and Execute methods to execute the following SQL command: 'delete from {id FDQA_ForAsync}'. The goal is to clear the table.
    2. Secondly, the sample fills the table using an Array DML execution. To this end, the sample:
      1. Sets the CommandText method to 'insert into {id FDQA_ForAsync}(id, name) values(:id, :name)'.
      2. Configures the Params properties.
      3. Uses the Execute method in Array DML mode.
    3. Thirdly, the sample sets up the asynchronous execution mode. It uses the selected radio button to set up the CmdExecMode property.
    4. Finally, the sample uses the Prepare and Open methods to execute the following SELECT statement: 'SELECT Count(*) FROM {id FDQA_ForAsync} a, {id FDQA_ForAsync} b GROUP BY a.name, b.name'. The sample executes the SQL statement according to the selected asynchronous execution mode.

The original post of Embarcadero can be found in the link below:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.IFDPhysCommand.Async_Sample

Head over and check out the full source code of the Delphi Async Sample app for Windows.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES