Creating the Server Side with DataSnap Server (InterBase Tutorial)
Go Up to Tutorial: Using an InterBase Database in a Delphi or C++ Application
You can implement a server in either Delphi or C++. This example shows both. The client does not need to be implemented in the same language. DataSnap allows you to have a Delphi server and a C++ client—or vice versa.
Follow these steps to create a server.
- Create a new project.
- For Delphi, choose File > New > VCL Form Application - Delphi to create a new Delphi project.
- For C++, choose File > New > VCL Form Application - C++Builder to create a new C++Builder project.
- Drag the following components from the Datasnap Server category of the Tool Palette onto the form:
- Set the form's Caption property to "ServerForm". Click File > Save All to save the project.
- For Delphi, save the file as ServerForm.pas and save the project as ServerDemo.dproj.
- For C++, save the file as ServerForm.cpp and save the project as ServerDemo.cbproj.
- Set the form's Caption property to "ServerForm". Click File > Save All to save the project.
- Your server form should now look like this:
- Follow these steps to link the three components together:
- Select the TDSServerClass component on the form. On the drop-down menu, set its Server property to the name of your TDSServer component, DSServer1 in this example.
- Select the TDSTCPServerTransport component on the form. On the drop-down menu, set its Server property to the name of your TDSServer component, DSServer1 in this example.
- Click the New items tool button
- Choose File > New > Other.
Add a new Delphi file called a ServerModule by clicking the tab Delphi Projects > DataSnap Server. Select ServerModule and click OK. Save the module as Un_ServerModule.pas.

C++
Add a new C++ file called a ServerModule by clicking the tab C++Builder Projects > DataSnap Server. Select ServerModule and click OK. Save the module as Un_ServerModule.cpp.

- A TSQLConnection component. Set its Name property to "EMPLOYEE_CONNECTION".
- A TSQLDataSet component. Change its Name property to "EMPLOYEE_TABLE".
When you dragged the table to the form, the two components were automatically connected. The SQLConnection property of EMPLOYEE_TABLE was set to the EMPLOYEE_CONNECTION TSQLConnection.
- Add a TDataSetProvider.
- Set its DataSet property to EMPLOYEE_TABLE on the drop-down menu.
- Change its name to ServerDataSetProvider1 to distinguish it from another TDataSetProvider that will be added later.
- Add a TSQLStoredProc component.
- Set its SQLConnection property to "EMPLOYEE_CONNECTION" on the drop-down menu.
- Set its StoredProcName property to "GET_EMP_PROJ" on the drop-down menu. "GET_EMP_PROJ" is one of the stored procedures in the Employee database. This stored procedure obtains a project ID associated with an employee number.
The data module now looks like the figure below. Although this figure shows a Delphi project, the C++Builder project looks very similar.
public
.public
section of Un_ServerModule may be called by the client.Delphi
Click the Code tab. In the type
section under public
, add this function declaration:
function callStoredProcedure (mylocalkey : Integer) : String;
CTRL-SHIFT-C
to create a stub for this function in the implementation
section.
C++
Click the Un_ServerModule.h tab to display the header file. Add the following function under public:
:
String _fastcall callStoredProcedure (int mylocalkey);
The callStoredProcedure
function calls a stored procedure with an integer parameter employee number (EMP_NO). The function obtains an AnsiString
project ID (PROJ_ID). The function sets the input parameter, executes the procedure, then retrieves the output parameter. The function parallels the function that will be written in the client in terms of parameters. We have already set the value of the StoredProcName property of the TSQLStoredProc component to the stored procedure name, "GET_EMP_PROJ".
Delphi
Add the following function to Un_ServerModule.pas':
function TDSServerModule1.callStoredProcedure(mylocalkey: Integer): String;
var
myString : String;
begin
SQLStoredProc1.ParamByName('EMP_NO').AsInteger := mylocalkey;
SQLStoredProc1.ExecProc;
myString := SQLStoredProc1.ParamByName('PROJ_ID').AsString;
result := myString;
end;
C++
Add this function after the other member functions in Un_ServerModule.cpp:
String _fastcall TDSServerModule1::callStoredProcedure (int mylocalkey)
{
String myString;
SQLStoredProc1->ParamByName("EMP_NO")->AsInteger = mylocalkey;
SQLStoredProc1->ExecProc();
myString = SQLStoredProc1->ParamByName("PROJ_ID")->AsString;
return myString;
}
Note that since the actual stored procedure parameter names, EMP_NO and PROJ_ID, are used, their ordinal value is obtained by ParamByName
.
Delphi
procedure TForm1.DSServerClass1GetClass(DSServerClass: TDSServerClass;
var PersistentClass: TPersistentClass);
begin
PersistentClass := TDSServerModule1;
end;
C++
void __fastcall TForm1::DSServerClass1GetClass(TDSServerClass *DSServerClass, TPersistentClass &PersistentClass)
{
PersistentClass = __classid(TDSServerModule1);
}
Note that the variable PersistentClass
is assigned to a class reference—not an object reference.
Provide the linkage needed in ServerForm to Un_ServerModule.
- For Delphi, go to the
uses
section of the ServerForm unit and add Un_ServerModule, so that TDSServerModule1 is recognized. - For C++, add this line after the other includes in ServerForm.cpp:
#include "Un_ServerModule.h"
This completes the server, which does two things:
- Provide database data that can be updated
- Execute a stored procedure and return a value