You can find the BatchErrorHandling sample project at:
- Start | Programs | Embarcadero RAD Studio Alexandria | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDQuery\ExecSQL\BatchErrorHandling
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
The BatchErrorHandling sample shows you how to handle errors when executing SQL Command Batches to execute multiple SQL commands. To this end, the sample implements two ways of handling the errors:
How to Use the Sample
- Navigate to the location given above and open
- Press F9 or choose Run > Run.
|File in Delphi||Contains|
The project itself.
The main form.
Before running the sample, the main components are configured at design time using the Object Inspector as follows:
- A 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
- A 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:
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 handler
- The 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
AExceptionparameter 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 loop
- The 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.
- Working with Commands (FireDAC)