1. 程式人生 > >DataSnap與FireDAC三層

DataSnap與FireDAC三層

adapt tsql method 應用服務 params ret conn ipp open

技術分享圖片

相交資料:

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三層