DataSnap与FireDAC三层

Posted jijm123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataSnap与FireDAC三层相关的知识,希望对你有一定的参考价值。

技术分享图片

相交资料:

http://blog.csdn.net/shuaihj/article/details/6129131
http://www.cnblogs.com/hnxxcxg/p/4007876.html
http://www.cnblogs.com/hnxxcxg/p/4008789.html
http://www.dfwlt.com/forum.php?mod=viewthread&tid=729 (生成Unit2)

 

PS:

右键点击TSQLConnection选择“Generate DataSnap client classes”生成,Uuit2。

 

服务端实例:

unit ServerMethodsUnit1;

interface

uses System.SysUtils, System.Classes, System.Json,
    Datasnap.DSServer, Datasnap.DSAuth, DataSnap.DSProviderDataModuleAdapter,
  Datasnap.Provider, FireDAC.Stan.ExprFuncs, FireDAC.Phys.SQLiteDef,
  FireDAC.UI.Intf, FireDAC.VCLUI.Wait, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
  FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Stan.Param, FireDAC.DatS,
  FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client, FireDAC.Comp.UI, FireDAC.Phys.SQLite,
  FireDAC.Stan.Storage,//未知
  FireDAC.Stan.StorageBin,//未知
  FireDAC.Stan.StorageJSON,//未知
  FireDAC.Stan.StorageXML,//未知
  Data.FireDACJSONReflect;//TFDJSONDataSets返回表使用

type
  TServerMethods1 = class(TDSServerModule)
    DataSetProvider1: TDataSetProvider; //要增加的控件
    FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink;//要增加的控件
    FDGUIxWaitCursor1: TFDGUIxWaitCursor; //要增加的控件
    FDConnection1: TFDConnection; //要增加的控件
    FDQuery1: TFDQuery;//要增加的控件
    DataSource1: TDataSource;//要增加的控件
  private
    { Private declarations }
  public
    { Public declarations }
    function EchoString(Value: string): string;
    function ReverseString(Value: string): string;
    //用户查询数据
    function GetDataTable(Value: string): TFDJSONDataSets;
    //用户删除记录
    function DeleteData(Value:string):string;

  end;

implementation


{$R *.dfm}

uses System.StrUtils;

function TServerMethods1.EchoString(Value: string): string;
begin
  Result := Value;
end;

function TServerMethods1.ReverseString(Value: string): string;
begin
  Result := System.StrUtils.ReverseString(Value);
end;

//用户查询数据
function TServerMethods1.GetDataTable(Value: string): TFDJSONDataSets;
begin
  //指定数据库
  FDConnection1.DriverName := SQLite;
  FDConnection1.Params.Add(Database=E:diarydate.db);
  //控件的联接情况
  FDQuery1.Connection := FDConnection1;
  DataSource1.DataSet := FDQuery1;
  //用户查询
  FDQuery1.SQL.Text := Value;//SELECT * FROM tproject
  //打开数据集
  FDConnection1.Open();
  FDQuery1.Open();
  //建立多个数据集返回集
  Result := TFDJSONDataSets.Create;
  //你也可以给自己的数据集起外名字
  //TFDJSONDataSetsWriter.ListAdd(Result, ‘A123‘, FDQuery1);
  TFDJSONDataSetsWriter.ListAdd(Result, FDQuery1);
end;

//用户删除记录
function TServerMethods1.DeleteData(Value: string): string;
begin
  //代表空
  Result := 0;
  //指定数据库
  FDConnection1.DriverName := SQLite;
  FDConnection1.Params.Add(Database=E:diarydate.db);
  //控件的联接情况
  FDQuery1.Connection := FDConnection1;
  DataSource1.DataSet := FDQuery1;
  //用户查询
  FDQuery1.SQL.Text := Value;//delete from tproject where fid=2
  //打开数据集
  FDConnection1.Open();
  FDQuery1.ExecSQL;
  //代表完成
  Result := 1;
end;

end.

客户端实例:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DBXDataSnap, IPPeerClient,
  Data.DBXCommon, Data.DB, Data.SqlExpr, Data.FMTBcd, Vcl.StdCtrls,
  FireDAC.Stan.Intf, FireDAC.Comp.DataMove,
  FireDAC.Stan.Option, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
  FireDAC.DApt,
  FireDAC.Comp.Client,
  FireDAC.Stan.Param, FireDAC.Stan.Error,
  FireDAC.Comp.DataSet, Vcl.Grids, Vcl.DBGrids, Datasnap.DBClient,
  Datasnap.DSConnect,
  Unit2,//DataSnap单元
  Data.FireDACJSONReflect,//TFDJSONDataSets使用
  FireDAC.Stan.StorageJSON,//未知
  FireDAC.Stan.StorageBin, //未知
  FireDAC.Stan.StorageXML,//未知
  FireDAC.Stan.Storage, //未知
  Datasnap.Provider;

type
  TForm1 = class(TForm)
    SQLConnection1: TSQLConnection; //要增加的控件
    SqlServerMethod1: TSqlServerMethod; //要增加的控件
    button1: TButton;  //要增加的控件
    button2: TButton;  //要增加的控件
    FDStanStorageJSONLink1: TFDStanStorageJSONLink; //要增加的控件
    FDStanStorageBinLink1: TFDStanStorageBinLink; //要增加的控件

    FDMemTable1: TFDMemTable; //要增加的控件
    DBGrid1: TDBGrid; //要增加的控件
    DataSource1: TDataSource; //要增加的控件
    Button3: TButton;  //要增加的控件
    procedure button1Click(Sender: TObject);
    procedure button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.button1Click(Sender: TObject);
begin
  SqlServerMethod1.ParamByName(Value).AsString:=Hello world;
  SqlServerMethod1.ExecuteMethod;
  ShowMessage(SqlServerMethod1.ParamByName(ReturnParameter).AsString);
end;

procedure TForm1.button2Click(Sender: TObject);
var
  oDSList: TFDJSONDataSets;
  oDataModel: TServerMethods1Client;
begin
  try
    //连接指定IP和Port的应用服务器
    SQLConnection1.Close;
    //这我用的是本机的,所以就没有写
    //SQLConnection1.Params.Values[‘HostName‘] := edtIP.Text;
    //SQLConnection1.Params.Values[‘Port‘] := edtPort.Text;
    try
      SQLConnection1.Open;
      try
         //创建应用服务器上的Sample Methods在客户端的实现类
        oDataModel := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
        //执行服务器上的方法
        FDMemTable1.Close;
        oDSList := oDataModel.GetDataTable(SELECT * FROM tproject);
        FDMemTable1.AppendData(TFDJSONDataSetsReader.GetListValue(oDSList, 0));
        FDMemTable1.Open;
      finally
        oDataModel.Free;
      end;
    except
      on E: Exception do
        ShowMessage(E.Message);
    end;
  finally
    SQLConnection1.Close;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  oDataModel: TServerMethods1Client;
  bIsTrue: string;
begin
  try
    //连接指定IP和Port的应用服务器
    SQLConnection1.Close;
    //这我用的是本机的,所以就没有写
    //SQLConnection1.Params.Values[‘HostName‘] := edtIP.Text;
    //SQLConnection1.Params.Values[‘Port‘] := edtPort.Text;
    try
      SQLConnection1.Open;
      try
        //创建应用服务器上的Sample Methods在客户端的实现类
        oDataModel := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
        bIsTrue := oDataModel.DeleteData(delete from tproject where fid=2);
        ShowMessage(bIsTrue);
      finally
        oDataModel.Free;
      end;
    except
      on E: Exception do
        ShowMessage(E.Message);
    end;
  finally
    SQLConnection1.Close;
  end;
end;

end.

 

以上是关于DataSnap与FireDAC三层的主要内容,如果未能解决你的问题,请参考以下文章

论DATASNAP结合FIREDAC的使用方法

Delphi XE FireDac 连接池

DATASNAP数据序列之FIREDAC的TFDJSONDataSets

调用DATASNAP+FIREDAC的远程方法有时会执行二次SQL或存储过程的BUG

firedac引擎DATASNAP多表查询和多表提交

Delphi操作WebService(两篇,疯狂delphi)