0% found this document useful (0 votes)
9 views4 pages

Creating_the_Server_Side_with_DataSnap_Server_(InterBase_Tutorial)

Uploaded by

xX Crystal Xx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views4 pages

Creating_the_Server_Side_with_DataSnap_Server_(InterBase_Tutorial)

Uploaded by

xX Crystal Xx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Show: Delphi C++

Display Preferences

Creating the Server Side with DataSnap Server (InterBase


Tutorial)
From RAD Studio

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.

1. 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.
2. Drag the following components from the Datasnap Server category of the Tool Palette onto the form:
TDSServer
TDSServerClass
TDSTCPServerTransport

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.

Your server form should now look like this:

3. 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.
4. Display the New Items dialog by doing either of the following:
Click the New items tool button
Choose File > New > Other.
Delphi
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.

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.

7. Place two additional components on Un_ServerModule's form:


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.
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:

ffuunnccttiioonn callStoredProcedure (mylocalkey : IInntteeggeerr) : SSttrriinngg;

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::

String _fastcall callStoredProcedure (int mylocalkey);

9. Write code for the function you just added.

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':

ffuunnccttiioonn TDSServerModule1.callStoredProcedure(mylocalkey: IInntteeggeerr): SSttrriinngg;


vvaarr
myString : SSttrriinngg;
bbeeggiinn
SQLStoredProc1.ParamByName('EMP_NO').AsInteger := mylocalkey;
SQLStoredProc1.ExecProc;
myString := SQLStoredProc1.ParamByName('PROJ_ID').AsString;
result := myString;
eenndd;

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.

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

pprroocceedduurree TForm1.DSServerClass1GetClass(DSServerClass: TDSServerClass;


vvaarr PersistentClass: TPersistentClass);
bbeeggiinn
PersistentClass := TDSServerModule1;
eenndd;

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"

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.

This completes the server, which does two things:

Provide database data that can be updated


Execute a stored procedure and return a value

Previous
Creating a Database Connection

Next
Creating a Rich-Client Interface

Retrieved from "http://docwiki.embarcadero.com/RADStudio/Seattle


/e/index.php?title=Creating_the_Server_Side_with_DataSnap_Server_(InterBase_Tutorial)&oldid=227763"
Categories: Delphi C++

This page was last modified on 10 June 2014, at 09:23.


Help Feedback (QP, email)

You might also like