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

Master A Powerful Way To Execute Multiple SQL Commands In Delphi With This Windows Sample

This sample demonstrates two ways of handling errors in an array DML execution.

To this end, the sample implements two ways of handling the errors:

  1. Implementing an OnError event.
  2. Using the EFDDBEngineException class on a loop.

Location

You can find the BatchErrorHandling sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object PascalDatabaseFireDACSamplesComp LayerTFDQueryExecSQLBatchErrorHandling
  • 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 BatchErrorHandling.dproj.
  2. Press F9 or choose Run > Run.

Files

File in DelphiContains
BatchErrorHandling.dproj
BatchErrorHandling.dpr
The project itself.
Unit1.pas
unit1.fmx
The main form.

Implementation

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

  • TFDConnection object named FDConnection1. This is FireDAC connection object that the sample uses to connect to a DBMS. The sample sets the ConnectionDefName property to Oracle_Demo.
  • TFDQuery object named FDQuery1. This component implements a dataset capable of executing SQL queries. To this end, the sample configures the following properties of the object:
    • The Connection property is set to FDConnection1 in order to specify the FireDAC connection object.
    • The SQL property of FDQuery1 is set to insert into batch_demo values (:id, :name).

When you run the application, you see two buttons labeled as: Using handler and Using loop. Both buttons implement two different ways of handling errors when executing SQL Command Batches. Click on any of the buttons to execute the corresponfing error handling technique. Both error handling techniques are implemented as follows:Using handlerThe sample uses the OnError event of FDQuery1. This event fires when an error happens while the dataset is communicating with a DBMS. The event checks if the AException parameter is an instance of EFDDBArrayExecuteError. Then, the sample uses the Offset and Action properties of EFDDBArrayExecuteError to fix the error.Note: After the execution, the property RowsAffected of FDQuery1 shows the number of successful executions.Using loopThe sample uses the Errors property of the EFDDBEngineException class to read TFDDBError objects, where each object corresponds to an error, warning, or message returned by a DBMS. This property is used in a loop in order to fix all the errors of the parameters array.

In the link below you will find the original post of the sample, as well as more link to other related information:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDQuery.BatchErrorHandling_Sample

Head over and check out the full source code for the Array DML error handling example on the Embarcadero GitHub.

With the use of Windows IDE, you can execute multiple SQL commands in Delphi when developing in Delphi or C++ environments. Try your Free Trial here.


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