User:Maria/Mobile Tutorial: Connecting to an Enterprise Database from a Mobile Client (iOS and Android) with FireDac

From RAD Studio
Jump to: navigation, search

Go Up to Mobile Tutorials: Mobile Application Development (iOS and Android)


Before starting this tutorial, you should read and perform the following tutorial session:

This tutorial describes how to connect to an Enterprise database from a mobile client application.

To connect to an Enterprise Database, you need to have a client library. In most cases, the client library is provided by the database vendor in DLL format. This strategy does not work well for mobile devices because no client library is available. To resolve this issue, you can develop a middle tier to connect to an Enterprise Database, and your mobile application can communicate with the middle tier.

RAD Studio provides the DataSnap framework with which you can develop the middle tier (and access the middle tier) with almost no coding required. This tutorial describes the steps to develop the middle tier and then develop the mobile client.

NTiersMobileAppDiagram.png

Creating the Middle Tier, a DataSnap Server

First, create a DataSnap server that exposes a table from a database server. This tutorial uses a DataSnap Server Forms Application as a DataSnap server.

Note: In this tutorial, the DataSnap server functions as the middle tier in a multi-tiered database application. You can easily create and later delete an instance of a DataSnap server. After you understand the basic steps, you can convert the middle tier to a Windows service application.

Create a DataSnap Server Application

  1. Create a new project. Choose File > New > Other and from the New Items dialog select Delphi Projects > DataSnap Server > DataSnap Server or C++Builder Projects > DataSnap Server > DataSnap Server in order to create a new Delphi or C++ project.
    DSServer iOS1.png

  2. The New DataSnap Server wizard appears and you need to follow its steps without modifying too many parameters.
    DSServer iOS2.png

    In the New DataSnap Server wizard:
    1. At first step, choose Forms Application as project type.
    2. At the second step, check VCL Application or FireMonkey Application as application type.
    3. At the third step, choose the TCP/IP protocol, Server Methods Class and Sample Methods from the Server Features list.
    4. At the fourth step, leave the default TCP/IP communications port to 211. This will ensure that the communication between the client and the server will pass through the default DataSnap port.
    5. At the final step (number five) select TDSServerModule as the ancestor for the Server Methods.
  3. Save the form unit as DataSnapServerUnit.pas / DataSnapServerUnit.cpp.
  4. On the DataSnapServerUnit change the Name property of the Form to DSServerForm.
  5. Save the server methods unit (by default as created by the Wizard: ServerMethodsUnit1) as ServerModuleUnit.pas/ ServerModuleUnit.cpp.
  6. Save the server container unit (by default as created by the Wizard: ServerContainerUnit1) as ServerContainerUnit.pas / ServerContainerUnit.cpp.
  7. Save the new project as DataSnapServerProject.droj / DataSnapServerProject.cbproj.
  8. Select ProjectGroup1 in the Project Manager, and save the project as DataSnapTutorialProjectGroup.groupproj.
    SaveAsDataSnapTutorialProjectGroup.png

Define the IB connection on the DataSnap Server

  1. Switch to the ServerContainerUnit.pas file and replace the ServerMethodsUnit1 uses clause in the implementation with: uses ServerModuleUnit, for Delphi, and replace #include "ServerMethodsUnit1.h" with #include "ServerModuleUnit.h" in ServerContainerUnit.cpp, for C++;
  2. Switch to the ServerModuleUnit.pas/cpp file.
  3. In the Form Designer, change the Name property of the Server Module to DSServerModule_EMPLOYEE.
  4. Configure the following components on the Server Module:
    • Drop a TFDConnection component on the Server Module, and set the following properties:
      TFDConnection encapsulates a FireDac connection to a database server.
      • Set the Name property to SQLConnection_EMPLOYEE.
      • Set the LoginPrompt property to False.
      • Set DriverName to IB.
      • Right-click the component and click Connection Editor... to open the FireDAC Connection Editor window.
      • Set the Database field to C:\Users\Public\Documents\Embarcadero\Studio\18.0\Samples\Data\EMPLOYEE.GDB
      • Set the User_Name and password. The values by default are User_Name = sysdba and Password = masterkey.
      • Change the Connected property to True. If you get an error, double-check the properties you just configured:
        FDtoIBConnection.png

    • Drop a TFDTable component on the Server Module, and set the following properties:
      TFDTable is a TDataSet that contains the table form the IB connection.
      • Set the Name property to EMPLOYEE_TABLE.
      • Set the Connection property to SQLConnection_EMPLOYEE.
      • Set the TableName property to EMPLOYEE.
      • Change the Active property to True. If you get an error, double-check the properties you just configured.

Note: This tutorial uses InterBase as an example. However, you can connect to any database server using the same steps. Select the proper driver, and other properties to point to your database.

Expose the Table from the DataSnap Server

You have just created a new Server Module that contains a TFDTable that packages data to the next layer. The next step is to expose the Server Module to the DataSnap client.

On the "ServerModuleUnit.pas/h" add the functions or methods that you want to expose as public to allowed them to be called by the client:

  • in Delphi:

Click the Code tab. In the type section under public, add this function declaration:

function GetTable: TDataSet; // This function returns a TDataSet that contains the table from the IB connection.
Use class completion by pressing CTRL-SHIFT-C to create a stub for this function in the implementation section.

Write code for the function you just added.

Add the following function to ServerModuleUnit.pas':

function TDSServerModule1.GetTable (): TDataSet;
begin
 EMPLOYEE_TABLE.Close;
 EMPLOYEE_TABLE.TableName := 'employee';
 EMPLOYEE_TABLE.Open;
 Result := EMPLOYEE_TABLE;
end;
  • In C++:

On the "ServerModuleUnit.h" add this function declaration:

TDataSet * __fastcall GetTable(); // This function returns a TDataSet that contains the table from the IB connection.

Write code for the function you just added on "ServerModuleUnit.cpp" .

TDataSet *__fastcall TDSServerModule_EMPLOYEE::GetTable()
{
	EMPLOYEE_TABLE->Close();
	EMPLOYEE_TABLE->TableName = "employee";
	EMPLOYEE_TABLE->Open();
	return EMPLOYEE_TABLE;
}
  1. In the Form Designer, open ServerContainerUnit.
  2. Select DSServerClass1, and update the existing event handler for the OnGetClass event. Add the following code to the DSServerClass1 event handler:

Delphi:

procedure TServerContainer1.DSServerClass1GetClass(DSServerClass: TDSServerClass;
  var PersistentClass: TPersistentClass);
begin
  PersistentClass := TDSServerModule_EMPLOYEE;
end;

C++ (only for iOS):

void __fastcall TServerContainer1::DSServerClass1GetClass(TDSServerClass *DSServerClass,
          TPersistentClass &PersistentClass)
{
	PersistentClass =  __classid(TDSServerModule_EMPLOYEE);
}

With this event handler, the DataSnap Server exposes providers as well as public methods in this class to a DataSnap client.

Run the DataSnap Server

Implementation of the DataSnap Server is complete. Right-click DataSnapServerProject.exe and select Run Without Debugging.

RunDataSnapServerProjectWihoutDebugging.png.

Now you can see the DataSnap server running on your Windows machine. Because this DataSnap server has no UI element, it looks like a blank form, and this is as expected at this point.

RunDataSnapServerRunningOnWindows.png

Creating a Mobile Application that Connects to the DataSnap Server

The next step is creating the mobile client application.

  1. In the Project Manager, right-click DataSnapTutorialProjectGroup, and select Add New Project.
    AddNewProjectToDataSnapTutorialProjectGroup.png

  2. Select FireMonkey Mobile Application on the Delphi Projects page:
    SelectFireMonkeyMobileApplication.png

  3. Save the new Unit as DataSnapClientUnit.pas/cpp.
  4. Save the new Project as DataSnapClientProject.droj/cbproj.
  5. Open DataSnapClientUnit, and change the Name property of the Form to DSClientForm.
  6. Drop the following components on the FireMonkey Mobile Form Designer:
    • TFDPhysDSDriverLink
    • TFDGUIxWaitCursor
    • TFDConnection component (FDConnection1)
      • Set the DriverName property to DS.
      • Right-click the component and click Connection Editor to open the FireDAC Connection Editor window.
      • Set the Server field with the IP address of the DataSnap Server. Do not use localhost or 127.0.0.1, use the ipconfig command to locate the IP address of the server.
      • Set the Port with the same value as the server, 211 value by default.
      • Set the LoginPrompt property to False.
      • Set the Connected property to True.
        If you see an error, please double-check the properties you have just set.
    • TFDStoredProc component (FDStoredProc1)
      The TFDConnection component provides connectivity to the DataSnap server using FireDAC.
    • TListBox component
      • Set the Align property to Client:
  7. Open the LiveBindings Designer and connect the data and user interface as follows:
    1. Click FULL_NAME in FDStoredproc1, and drag the mouse cursor to Item.Text in ListBox1:
      FDandDSMobile.png

    2. Now you have created and configured the DataSnap Client on the mobile platform. You should be able to see the data coming from the DataSnap server in the IDE:

Run Your Application on the mobile platform

Now your application is ready to run.

In the Project Manager, select the mobile target platform, and run your application. You should be able to browse data just as you do within the IDE.

See Also