一般http访问的地址是
http://localhost:8099/datasnap/rest/TServerMethods1/EchoString/abc
一、用FDConnection1连接Datasnap服务器
FireDAC 连接Datasnap服务端。这个是tcp协议连接通讯,长连接。服务端不是没个方法都建立实例释放实例,而是连接的时候建立,中间调用多少个方法都不释放实例,连接断开才释放。
不是。net的web api或者Datasnap 的 webmodule,纯粹是tcp。
DriverID=DS Protocol=tcp/ip Server=127.0.0.1 Port=211 User_Name=dsusr Password=123
http://docwiki.embarcadero.com/RADStudio/Berlin/en/Connect_to_DataSnap_Server_(FireDAC)
fdstoreproc 执行返回数据
FDStoredProc1.Close;
FDStoredProc1.Unprepare;
FDStoredProc1.StoredProcName := 'TServerMethods1.QueryData';
FDStoredProc1.Prepare;
FDStoredProc1.ParamByName('sql').Value := 'select * from MyTable';
FDStoredProc1.open;
FDMemTable1.Close;
FDMemTable1.Data := FDStoredProc1.Data;
如果是字符串参数及返回字符串参数
FDStoredProc1.Params.Items[0].Value := 'hello'; //入参
Self.Caption := FDStoredProc1.Params.Items[1].Value; //返回参数
也可以通过ParamByName对Datasnap server的方法传参,是不是很方便?
FDStoredProc1.Params.ParamByName('value').Value:= 'hello';
FDStoredProc1.Params.ParamByName('retvlue').Value;
self.Caption:= FDStoredProc1.Params.Items[0].Name;
self.Caption:= FDStoredProc1.Params.Items[1].Name;
procedure TDepartmentsClientForm.Button3Click(Sender: TObject); begin FDStoredProc1.Close; FDStoredProc1.Unprepare; FDStoredProc1.StoredProcName := 'TServerMethods1.ReverseString'; FDStoredProc1.Prepare; FDStoredProc1.Params.Items[0].Value := 'hello'; FDStoredProc1.Params.Count; FDStoredProc1.ExecProc; Self.Caption := FDStoredProc1.Params.Items[1].Value; end; procedure TDepartmentsClientForm.Button4Click(Sender: TObject); begin FDStoredProc1.Close; FDStoredProc1.Unprepare; FDStoredProc1.StoredProcName := 'TServerMethods1.EchoString'; FDStoredProc1.Prepare; FDStoredProc1.Params.Items[0].Value := 'hello'; FDStoredProc1.Params.Count; FDStoredProc1.ExecProc; Self.Caption := FDStoredProc1.Params.Items[1].Value; end;
二、TSQLConnection,(该控件其实也支持android平台)
参考官方的例子
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Connecting_the_Client_to_DataSnap_Server
也支持http。
D:UsersPublicDocumentsEmbarcaderoStudio14.0SamplesCPPDataSnapSimpleDataSnapDemoCppDataSnapClientCppDataSnapClientApp.cbproj
这个也是tcp通讯,原理同上。
SQLConnection控件,Params,
DriverName=DataSnap
HostName=localhost
port=211
void __fastcall TForm2::CheckBox1Change(TObject *Sender) { SQLConnection1->Connected = CheckBox1->IsChecked; } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { TServerMethods1Client *Temp; Temp = new TServerMethods1Client(SQLConnection1->DBXConnection); try { Label1->Text = Temp->EchoString(Edit1->Text); } __finally { delete Temp; } } //--------------------------------------------------------------------------- void __fastcall TForm2::Button2Click(TObject *Sender) { TServerMethods1Client *Temp; Temp = new TServerMethods1Client(SQLConnection1->DBXConnection); try { Label1->Text = Temp->ReverseString(Edit1->Text); } __finally { delete Temp; } }
DataSnapClientClasses.h #ifndef DataSnapClientClassesH #define DataSnapClientClassesH #include "DBXCommon.hpp" #include "Classes.hpp" #include "SysUtils.hpp" #include "DB.hpp" #include "SqlExpr.hpp" #include "DBXDBReaders.hpp" #include "DBXCDSReaders.hpp" class TServerMethods1Client : public TObject { private: TDBXConnection *FDBXConnection; bool FInstanceOwner; TDBXCommand *FEchoStringCommand; TDBXCommand *FReverseStringCommand; public: __fastcall TServerMethods1Client(TDBXConnection *ADBXConnection); __fastcall TServerMethods1Client(TDBXConnection *ADBXConnection, bool AInstanceOwner); __fastcall ~TServerMethods1Client(); System::UnicodeString __fastcall EchoString(System::UnicodeString value); System::UnicodeString __fastcall ReverseString(System::UnicodeString value); }; #endif //--------------------------------------------------------------------------- // This software is Copyright (c) 2014 Embarcadero Technologies, Inc. // You may only use this software if you are an authorized licensee // of an Embarcadero developer tools product. // This software is considered a Redistributable as defined under // the software license agreement that comes with the Embarcadero Products // and is subject to that software license agreement. //--------------------------------------------------------------------------- // // Created by the DataSnap proxy generator. // 1/31/2013 2:39:56 PM // #include "DataSnapClientClasses.h" System::UnicodeString __fastcall TServerMethods1Client::EchoString(System::UnicodeString value) { if (FEchoStringCommand == NULL) { FEchoStringCommand = FDBXConnection->CreateCommand(); FEchoStringCommand->CommandType = TDBXCommandTypes_DSServerMethod; FEchoStringCommand->Text = "TServerMethods1.EchoString"; FEchoStringCommand->Prepare(); } FEchoStringCommand->Parameters->Parameter[0]->Value->SetWideString(value); FEchoStringCommand->ExecuteUpdate(); System::UnicodeString result = FEchoStringCommand->Parameters->Parameter[1]->Value->GetWideString(); return result; } System::UnicodeString __fastcall TServerMethods1Client::ReverseString(System::UnicodeString value) { if (FReverseStringCommand == NULL) { FReverseStringCommand = FDBXConnection->CreateCommand(); FReverseStringCommand->CommandType = TDBXCommandTypes_DSServerMethod; FReverseStringCommand->Text = "TServerMethods1.ReverseString"; FReverseStringCommand->Prepare(); } FReverseStringCommand->Parameters->Parameter[0]->Value->SetWideString(value); FReverseStringCommand->ExecuteUpdate(); System::UnicodeString result = FReverseStringCommand->Parameters->Parameter[1]->Value->GetWideString(); return result; } __fastcall TServerMethods1Client::TServerMethods1Client(TDBXConnection *ADBXConnection) { if (ADBXConnection == NULL) throw EInvalidOperation("Connection cannot be nil. Make sure the connection has been opened."); FDBXConnection = ADBXConnection; FInstanceOwner = True; } __fastcall TServerMethods1Client::TServerMethods1Client(TDBXConnection *ADBXConnection, bool AInstanceOwner) { if (ADBXConnection == NULL) throw EInvalidOperation("Connection cannot be nil. Make sure the connection has been opened."); FDBXConnection = ADBXConnection; FInstanceOwner = AInstanceOwner; } __fastcall TServerMethods1Client::~TServerMethods1Client() { delete FEchoStringCommand; delete FReverseStringCommand; }
三、DSRestConnection
这个是http调用,rest风格,每一次方法的调用都是一次实例化,方法,释放的3个过程。
就是 web api/rest风格,调用方法类似SQLConnection,生个各个方法的定义。
只支持http和https两种通讯协议。调用datasnap Rest服务。
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Connecting_the_Client_to_DataSnap_Server
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Tutorial:_Using_a_REST_DataSnap_Server_with_an_Application
self.DSRestConnection1.Host:='127.0.0.1'; self.DSRestConnection1.Port:=8083 ; self.DSRestConnection1.Protocol:=''//http https TServerMethods1Client这个生成很多方法 ClientModule2.ServerMethods1Client.GetDepartmentNames();
function TServerMethods1Client.GetDepartmentNames(const ARequestFilter: string): TFDJSONDataSets; begin if FGetDepartmentNamesCommand = nil then begin FGetDepartmentNamesCommand := FConnection.CreateCommand; FGetDepartmentNamesCommand.RequestType := 'GET'; FGetDepartmentNamesCommand.Text := 'TServerMethods1.GetDepartmentNames'; FGetDepartmentNamesCommand.Prepare(TServerMethods1_GetDepartmentNames); end; FGetDepartmentNamesCommand.Execute(ARequestFilter); if not FGetDepartmentNamesCommand.Parameters[0].Value.IsNull then begin FUnMarshal := TDSRestCommand(FGetDepartmentNamesCommand.Parameters[0].ConnectionHandler).GetJSONUnMarshaler; try Result := TFDJSONDataSets(FUnMarshal.UnMarshal(FGetDepartmentNamesCommand.Parameters[0].Value.GetJSONValue(True))); if FInstanceOwner then FGetDepartmentNamesCommand.FreeOnExecute(Result); finally FreeAndNil(FUnMarshal) end end else Result := nil; end;
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Getting_Started_with_Java_Android_DataSnap_Mobile_Connector
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Getting_Started_with_DataSnap_Mobile_Connectors
也就是客户端调用,是通过生成服务方法代理
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Generating_DataSnap_Server_Method_Proxies
Generating DataSnap Server Method Proxies
Go Up to DataSnap Server Application
Proxy DataSnap classes are needed in client applications to invoke server methods. In order to generate server methods, drop a TDSRestConnection in a form and connect to a running DataSnap server in design mode. Right-click the TDSRestConnection component and select Generate DataSnap Client Classes.
The proxy unit is added to the current project and each exposed method
can be used to invoke the corresponding server method. When generating a
Delphi proxy for the server, that proxy should have on it the useful DSAdmin methods. These DSAdmin
methods are any functions or procedures publicly declared and exposed
to the user, as well as any visible methods in the classes that DSAdmin extends.
If the server method signature changes or methods are added or removed, the same option will refresh the proxy unit.
后续参考http://blog.csdn.net/cb168/article/details/14454499 客户端部分