Delphi uses Datasnap three layer application development, we have accumulated several techniques, summarized as follows:
1, (recommended!) Use TDatasetProvider in Datasnap server, the client uses TDSProviderConnection
1) Datasnap Server, can use TCP / IP, Http communication. TDatasetProvider able to provide "meta-data and business data."
Recommended: using (FireDac technology) TFDConnection connect to the database, to pass TFDQuery / TFDTable the TDataSetProvider provide data sets; (Dharana new version of the database!)
Note: can also be employed (DBX technology) TSQLConnect, TSQLDataSet / TSQLQuery / TSQLTable / TSQLStoredProc, TDataSetProvider to provide a data set;
2) use of a client (DBExpress is technology DBX ) + TDSProviderConnection the TSQLConnection == "the TClientDataSet ==" the TDataSource
TSQLConnection set DataSnapCONNECTION, driving the DataSnap;
TDSProviderConnection provided: Example TSQLConnection (driving DataSnap); ServerClassName set "class name" the DataSnap server providing the service;
TClientDataSet set: RemoteServer TDSProviderConnection to the example, ProviderName is a corresponding "class" service side TDatasetProvider instance.
Tip. 1: (. 1 the FRM ) can bind the TClientDataSet TStringGrid, or (2 the VCL ) connected TDBGrid by increasing TDataSource.
Tip 2: Change the client TClientDataSet, and how to return the server and the client query data sets, sorting, filtering,
The proposed reference " Delphi2006-efficient dbExpress database development " book; "Delphi 10.1 Berlin DataSnap Development Handbook," first chapter also introduces this technology.
3) if a T DataSetProvider, can only provide a a Dataset, to be established if the master Master / Detail from the data set, requires the following way:
TFDQuery established using two data sets, a main Master, the other main Masterdata property data set.
4) (well, for reference)! Server can be only one: TFDConnection + + TFDQuery the TDataSetProvider provide data to the client,
But TDatasetProvider property Options Select poAllowCommandText is True , which allows TFDQuery does not manage its SQL value,
Receive SQL directly from the client, and the client returns the query data collection terminal.
TClientDataSet client by CommandText property setting SQL commands, parameters, and parameter values acquired from the Params, sent to the SQL server, and receives return data.
This allows a server TDatasetProvider, serving multiple clients. Of course, a one-time session Session more appropriate, or data acquisition is completed, the connection is disconnected SQLConn.
Reference " the DataSnap three main ClientDataSet from the operation table " from the setting table to get the master! ! !
2, in use TFDQuery Datasnap server, or a client using TSQLConnection TDSRestConnection, data exchange TStream
Reference: http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/DataSnap.FireDAC_DBX_Sample ( DataSnap United FireDAC )
1) Datasnap Server, can use TCP / IP, Http communication.
TFDConnection connection database, the data set provided by TFDQuery (provided + TFDSchemaAdapter), == "TDataSource (primary)
By TFDQuery (provided + TFDSchemaAdapter, provided MasterSource, MasterFields as "primary data source / ID") offers data sets,
== "TDataSource (from)
Publication service class (Class) method sets transmission data to TStream type manner, by TFDSchemaAdapter transmit data, receive updates, and written to the database.
function TServerMethods.StreamGet: TStream;
the begin
the Result: = TMemoryStream.Create;
the try
qCustomers.Close; // master data set
qCustomers.Open;
qOrders.Close; // data set from
qOrders.Open;
FDSchemaAdapter .SaveToStream (the Result, TFDStorageFormat.sfBinary);
Result.Position: 0 =;
the finally
//
than;
than;
And receiving method:
TServerMethods.StreamPost function (AStream: of TStream): String;
var
LMemStream: TMemoryStream;
LErrors: Integer;
the begin
the Result: = '';
// get the data stream from a client Retreive Stream from Client Entire
LMemStream: = CopyStream (AStream); / / reproducing data streams
LMemStream.Position: = 0;
the try
FDSchemaAdapter .LoadFromStream (LMemStream, TFDStorageFormat.sfBinary);
LErrors: = FDSchemaAdapter.ApplyUpdates
finally
LMemStream.Free;
if LErrors > 0 then
Result := Format(sErrorsOnApplyUpdates , [GenerateErrorMessage]);
end;
end;
2) The client can use two connections: 1) TSQLConnection (DBX mode); 2) TDSRestConnection (Rest mode)
Can be generated by both (right dot) server proxy class (Generate Datasnap Client Classes), call proxy class method, the server acquires data stream.
TFDTableAdapter (+ same TFDSchemaAdapter, set DatSTableName to the service side of TFDQuery name ) == "TMemTable ==" TDataSource (main - Customer)
TFDTableAdapter (+ same TFDSchemaAdapter) set DatSTableName to the service side of TFDQuery name ) == "TMemTable ==" TDataSource (from - orders)
How to get the data, reference example! (Omitted ...) binding GRID.
LMemStream: = TServerMethodsClient (ServerConnection) .StreamGet proxy class through the connector assembly, the server acquires data flow of TStream;
FDSchemaAdapter .LoadFromStream (LMemStream, TFDStorageFormat.sfBinary) loading the data set from the stream.
3, (considering!) Rest Datasnap application server, the Http communication, TFDJsonDataSets exchange data with format TFDJsonDelta
Examples Chapter 9 (9.3) 3 Use TFDJsonDataSets function reference "Delphi 10.1 Berlin DataSnap Development Manual"
4, (recommended) using Restful Rest Client connection service, not limited to (Delphi generated), attention: TRESTResponseDataSetAdapter applications.
"Delphi 10.1 Berlin FireDAC Database Development Manual" P109 3-1-2 first chapter describes the application, processing the data using TFDMemTable Rest obtained.
Use TRESTResponseDataSetAdapter the TRESTResponse data to TFDMemTable, contains metadata information.
Official Example: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/REST_Client_Library