FireDAC.DAptLayerGettingStarted Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample shows how to work with table adapters.

Location

You can find the GettingStarted sample project at:

Description

The GettingStarted sample implements some functionalities working with table adapters using the TFDSchemaAdapter and the TFDTableAdapter classes.

How to Use the Sample

  1. Navigate to the location given above and open GettingStarted.dproj.
  2. Press F9 or choose Run > Run.
  3. Click on the Use Connection Definition combo box and select an option.

Files

File in Delphi Contains

GettingStarted.dproj
GettingStarted.dpr

The project itself.

fGettingStarted.pas
fGettingStarted.fmx

The main form.

Implementation

The sample implements the following features related with table adapters.

Create schema manager

It is not required, you can create stand-alone table adapter.

  FDCreateInterface(IFDDAptSchemaAdapter, oSchAdapt);

Create and assign DatSManager to schema adapter

It is not required. If it is not assigned, FireDAC automatically allocates DatSManager. Moreover, you may assign the already existing DatSManager.

  var
    oSchAdapt: IFDDAptSchemaAdapter;  
  begin
    // create and assign DatSManager to schema adapter
    oSchAdapt.DatSManager := TFDDatSManager.Create;
    // ...

Create table adapter

It will handle hippers result set and put it into MappedShippers DatSTable.

  var
    oAdapt: IFDDAptTableAdapter;
  begin
    // create table adapter
    oAdapt := oSchAdapt.TableAdapters.Add(EncodeName('Shippers'), 'MappedShippers');
    // ...

Map result set columns to DatSTable columns

It is not required. By default, FireDAC handles all result set columns using their names.

  oAdapt.ColumnMappings.Add('ShipperID',   'MappedShipperID');
  oAdapt.ColumnMappings.Add('CompanyName', 'MappedCompanyName');
  oAdapt.ColumnMappings.Add('Phone',       'MappedPhone');

Create and assign command to table adapter

To this end, the sample uses the CreateCommand and SelectCommand properties.

  FConnIntf.CreateCommand(oComm);
  oAdapt.SelectCommand := oComm;
  oAdapt.SelectCommand.Prepare('select * from {id Shippers}');

Create DatSTable to fetch result set

The sample uses the Define method to create a DatSTable.

  oAdapt.Define;

Fetch result set

The sample uses the Fetch method.

  oAdapt.Fetch(True);

Get DatSTable and print it

  var
    oTab: TFDDatSTable;  
  begin  
    // ...
    oTab := oAdapt.DatSTable;
    PrintRows(oTab, Console.Lines);
    // ...

Uses

See Also

Samples

Personal tools
Assistance