zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    HDU5890:Eighty seven(Bitset优化背包)
    AtCoder3857:Median Sum (Bitset优化背包&&对称性求中位数)
    POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)
    Gym
    POJ2443 Set Operation (基础bitset应用,求交集)
    POJ2976:Dropping tests(01分数规划入门)
    HihoCoder1084: 扩展KMP(二分+hash,求T串中S串的数量,可以失配一定次数)
    扩展KMP(占位)
    MySQL主备模式的数据一致性解决方案
    MaxCompute问答整理之6月
  • 原文地址:https://www.cnblogs.com/jijm123/p/9967033.html
Copyright © 2011-2022 走看看