Show/Hide Toolbars

TMS XData Documentation

Navigation: Getting Started > Creating the Server Manually

Example 3: MS SQL Server with FireDAC (using TDataModule)

Scroll Prev Top Next More

The following example illustrates how to start an XData server at address "http://localhost:2001/tms/music" using FireDac to connect to any database (in the example, a MS SQL Server database). Compared to Example 2, this example uses a TFDConnection dropped onto a TDataModule to configure the database connection at design-time. It uses an unit named "AureliusEntities" which is not shown here for simplification. Such a unit should just contain the Aurelius mapped classes that will be exposed by the server. The connection pool is configured to hold up to 15 connections to the database.


Thus, consider you have a data module with an existing configured database connection using FireDac:





You can use such a configuration and create one instance of that data module when required by the server. You can use the TFDConnection component from it.


program FireDacConsoleServer;
  AureliusEntities in '..\common\AureliusEntities.pas',
  DBDataModuleUnit in 'DBDataModuleUnit.pas' {DBDataModule: TDataModule};
procedure StartServer;
  Server: THttpSysServer;
  Server := THttpSysServer.Create;
      TDBConnectionPool.Create(15, TDBConnectionFactory.Create(
        function: IDBConnection
          DBDataModule: TDBDataModule;
          // Example using FireDac and Data Module
          DBDataModule := TDBDataModule.Create(nil);
          Result := TFireDacConnectionAdapter.Create(DBDataModule.FDConnection1, DBDataModule);
    WriteLn('Server started. Press ENTER to stop.');