Creating_the_Server_Side_with_DataSnap_Server_(InterBase_Tutorial)
Creating_the_Server_Side_with_DataSnap_Server_(InterBase_Tutorial)
Display Preferences
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.
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.
5. Un_ServerModule is a Data Module to which you can add various database controls. You can also add public methods to its
code using the Code Editor, as shown later.
6. You can add components to a project by dragging items from the Data Explorer. On the left pane of RAD Studio, click the Data
Explorer tab. If the INTERBASE tab isn't open, open it. Under the INTERBASE tab, open EMPLOYEE and then Tables. Drag the
EMPLOYEE table to the Un_server_module form, which results in two new dbExpress components being added to the form:
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.
The data module now looks like the figure below. Although this figure shows a Delphi project, the C++Builder project looks
very similar.
8. Add the functions to Un_ServerModule that you want to expose as public.
Note: All server methods in the 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:
Use class completion by pressing 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::
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
C++
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.
10. Go back to ServerForm by clicking the ServerForm tab in the RAD Studio file list. Click the Design tab, then click on the form.
Select the TDSServerClass component. In the Object Inspector for the TDSServerClass component, click the Events tab and
double-click on OnGetClass. This event handler code determines which server class the server uses:
Delphi
C++
{
PersistentClass = __classid(TDSServerModule1);
}
Note that the variable PersistentClass is assigned to a class reference—not an object reference.
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"
11. Save the unit. Build the server project and fix any errors, but do not run the server at this time.
12. Click on the Project Manager tab in RAD Studio. Save the project group by right-clicking the project group and clicking Save
Project Group. Save the project group as DSProj.groupproj. In the next section we will add another project to this project
group.
Previous
Creating a Database Connection
Next
Creating a Rich-Client Interface