FireDAC allows the insertion of a new record with an auto-incrementing column and getting back a new value of this column. That works for immediate updates, as well as for cached updates. Depending on the DBMS, the auto-incrementing fields may be implemented either using a special IDENTITY (or similar) column data type, or using a generator (or a sequence) and a table trigger
Table of Contents
Location
You can find the AutoincFields sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\DApt Layer\Autoinc fields
- 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 AutoincFields sample shows you how to work with auto-incremental fields using FireDAC. To this end, the sample uses the TFDDatSColumn class to control the client-side and the server-side auto-incrementing fields. Concretely, the sample uses the TFDDatSColumn.ServerAutoIncrement property of TFDDatSColumn to specify when the DBMS generates an auto-incrementing value for the new record column.
How to Use the Sample
- Navigate to the location given above and open
AutoInc.dproj
. - Press F9 or choose Run > Run.
- Click on the Use Connection Definition combo box and select an option.
Files
File in Delphi | Contains |
---|---|
AutoInc.dproj AutoInc.dpr | The project itself. |
fAutoInc.pas fAutoInc.fmx | The main form. |
Implementation
The sample implements the following features.
Create schema adapter
To this end, the sample uses FDCreateInterface. See the following code:
[crayon-6768e2bceba53278109240/]Add master table adapter
The sample adds the master table adapter and sets some configuration properties, such as the ServerAutoIncrement property. See the following code:
[crayon-6768e2bceba5d892433574/]Note: setting the ServerAutoIncrement property to True
automatically assigns the AutoIncrementSeed and AutoIncrementStep properties to -1
.
Add detail table adapter
The sample adds a detail table adapter and sets up some features. To this end, the sample adds the following code:
[crayon-6768e2bceba5f949883536/]Add constraints to the DatSManager
To this end, the sample uses the Constraints property of TFDDatSTable. See the following code:
[crayon-6768e2bceba60199218656/]Add new row to the master table
[crayon-6768e2bceba62405403160/]Post changes to RDBMS
Finally, the sample updates the changes with the Update method. Then, the sample uses the Reconcile method to obtain the error information for each record that could not be applied.
[crayon-6768e2bceba63220303640/]To see the original post, please refer to the link below:
http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.AutoincFields_Sample