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。

 

服务端实例:

 1 unit ServerMethodsUnit1;
 2 
 3 interface
 4 
 5 uses System.SysUtils, System.Classes, System.Json,
 6     Datasnap.DSServer, Datasnap.DSAuth, DataSnap.DSProviderDataModuleAdapter,
 7   Datasnap.Provider, FireDAC.Stan.ExprFuncs, FireDAC.Phys.SQLiteDef,
 8   FireDAC.UI.Intf, FireDAC.VCLUI.Wait, FireDAC.Stan.Intf, FireDAC.Stan.Option,
 9   FireDAC.Stan.Error, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
10   FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Stan.Param, FireDAC.DatS,
11   FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
12   FireDAC.Comp.Client, FireDAC.Comp.UI, FireDAC.Phys.SQLite,
13   FireDAC.Stan.Storage,//未知
14   FireDAC.Stan.StorageBin,//未知
15   FireDAC.Stan.StorageJSON,//未知
16   FireDAC.Stan.StorageXML,//未知
17   Data.FireDACJSONReflect;//TFDJSONDataSets返回表使用
18 
19 type
20   TServerMethods1 = class(TDSServerModule)
21     DataSetProvider1: TDataSetProvider; //要增加的控件
22     FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink;//要增加的控件
23     FDGUIxWaitCursor1: TFDGUIxWaitCursor; //要增加的控件
24     FDConnection1: TFDConnection; //要增加的控件
25     FDQuery1: TFDQuery;//要增加的控件
26     DataSource1: TDataSource;//要增加的控件
27   private
28     { Private declarations }
29   public
30     { Public declarations }
31     function EchoString(Value: string): string;
32     function ReverseString(Value: string): string;
33     //用户查询数据
34     function GetDataTable(Value: string): TFDJSONDataSets;
35     //用户删除记录
36     function DeleteData(Value:string):string;
37 
38   end;
39 
40 implementation
41 
42 
43 {$R *.dfm}
44 
45 uses System.StrUtils;
46 
47 function TServerMethods1.EchoString(Value: string): string;
48 begin
49   Result := Value;
50 end;
51 
52 function TServerMethods1.ReverseString(Value: string): string;
53 begin
54   Result := System.StrUtils.ReverseString(Value);
55 end;
56 
57 //用户查询数据
58 function TServerMethods1.GetDataTable(Value: string): TFDJSONDataSets;
59 begin
60   //指定数据库
61   FDConnection1.DriverName := 'SQLite';
62   FDConnection1.Params.Add('Database=E:\diarydate.db');
63   //控件的联接情况
64   FDQuery1.Connection := FDConnection1;
65   DataSource1.DataSet := FDQuery1;
66   //用户查询
67   FDQuery1.SQL.Text := Value;//SELECT * FROM tproject
68   //打开数据集
69   FDConnection1.Open();
70   FDQuery1.Open();
71   //建立多个数据集返回集
72   Result := TFDJSONDataSets.Create;
73   //你也可以给自己的数据集起外名字
74   //TFDJSONDataSetsWriter.ListAdd(Result, 'A123', FDQuery1);
75   TFDJSONDataSetsWriter.ListAdd(Result, FDQuery1);
76 end;
77 
78 //用户删除记录
79 function TServerMethods1.DeleteData(Value: string): string;
80 begin
81   //代表空
82   Result := '0';
83   //指定数据库
84   FDConnection1.DriverName := 'SQLite';
85   FDConnection1.Params.Add('Database=E:\diarydate.db');
86   //控件的联接情况
87   FDQuery1.Connection := FDConnection1;
88   DataSource1.DataSet := FDQuery1;
89   //用户查询
90   FDQuery1.SQL.Text := Value;//delete from tproject where fid=2
91   //打开数据集
92   FDConnection1.Open();
93   FDQuery1.ExecSQL;
94   //代表完成
95   Result := '1';
96 end;
97 
98 end.

 

客户端实例:

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DBXDataSnap, IPPeerClient,
  8   Data.DBXCommon, Data.DB, Data.SqlExpr, Data.FMTBcd, Vcl.StdCtrls,
  9   FireDAC.Stan.Intf, FireDAC.Comp.DataMove,
 10   FireDAC.Stan.Option, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
 11   FireDAC.DApt,
 12   FireDAC.Comp.Client,
 13   FireDAC.Stan.Param, FireDAC.Stan.Error,
 14   FireDAC.Comp.DataSet, Vcl.Grids, Vcl.DBGrids, Datasnap.DBClient,
 15   Datasnap.DSConnect,
 16   Unit2,//DataSnap单元
 17   Data.FireDACJSONReflect,//TFDJSONDataSets使用
 18   FireDAC.Stan.StorageJSON,//未知
 19   FireDAC.Stan.StorageBin, //未知
 20   FireDAC.Stan.StorageXML,//未知
 21   FireDAC.Stan.Storage, //未知
 22   Datasnap.Provider;
 23 
 24 type
 25   TForm1 = class(TForm)
 26     SQLConnection1: TSQLConnection; //要增加的控件
 27     SqlServerMethod1: TSqlServerMethod; //要增加的控件
 28     button1: TButton;  //要增加的控件
 29     button2: TButton;  //要增加的控件
 30     FDStanStorageJSONLink1: TFDStanStorageJSONLink; //要增加的控件
 31     FDStanStorageBinLink1: TFDStanStorageBinLink; //要增加的控件
 32 
 33     FDMemTable1: TFDMemTable; //要增加的控件
 34     DBGrid1: TDBGrid; //要增加的控件
 35     DataSource1: TDataSource; //要增加的控件
 36     Button3: TButton;  //要增加的控件
 37     procedure button1Click(Sender: TObject);
 38     procedure button2Click(Sender: TObject);
 39     procedure Button3Click(Sender: TObject);
 40   private
 41     { Private declarations }
 42   public
 43     { Public declarations }
 44   end;
 45 
 46 var
 47   Form1: TForm1;
 48 
 49 implementation
 50 
 51 {$R *.dfm}
 52 
 53 procedure TForm1.button1Click(Sender: TObject);
 54 begin
 55   SqlServerMethod1.ParamByName('Value').AsString:='Hello world';
 56   SqlServerMethod1.ExecuteMethod;
 57   ShowMessage(SqlServerMethod1.ParamByName('ReturnParameter').AsString);
 58 end;
 59 
 60 procedure TForm1.button2Click(Sender: TObject);
 61 var
 62   oDSList: TFDJSONDataSets;
 63   oDataModel: TServerMethods1Client;
 64 begin
 65   try
 66     //连接指定IP和Port的应用服务器
 67     SQLConnection1.Close;
 68     //这我用的是本机的,所以就没有写
 69     //SQLConnection1.Params.Values['HostName'] := edtIP.Text;
 70     //SQLConnection1.Params.Values['Port'] := edtPort.Text;
 71     try
 72       SQLConnection1.Open;
 73       try
 74          //创建应用服务器上的Sample Methods在客户端的实现类
 75         oDataModel := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
 76         //执行服务器上的方法
 77         FDMemTable1.Close;
 78         oDSList := oDataModel.GetDataTable('SELECT * FROM tproject');
 79         FDMemTable1.AppendData(TFDJSONDataSetsReader.GetListValue(oDSList, 0));
 80         FDMemTable1.Open;
 81       finally
 82         oDataModel.Free;
 83       end;
 84     except
 85       on E: Exception do
 86         ShowMessage(E.Message);
 87     end;
 88   finally
 89     SQLConnection1.Close;
 90   end;
 91 end;
 92 
 93 procedure TForm1.Button3Click(Sender: TObject);
 94 var
 95   oDataModel: TServerMethods1Client;
 96   bIsTrue: string;
 97 begin
 98   try
 99     //连接指定IP和Port的应用服务器
100     SQLConnection1.Close;
101     //这我用的是本机的,所以就没有写
102     //SQLConnection1.Params.Values['HostName'] := edtIP.Text;
103     //SQLConnection1.Params.Values['Port'] := edtPort.Text;
104     try
105       SQLConnection1.Open;
106       try
107         //创建应用服务器上的Sample Methods在客户端的实现类
108         oDataModel := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
109         bIsTrue := oDataModel.DeleteData('delete from tproject where fid=2');
110         ShowMessage(bIsTrue);
111       finally
112         oDataModel.Free;
113       end;
114     except
115       on E: Exception do
116         ShowMessage(E.Message);
117     end;
118   finally
119     SQLConnection1.Close;
120   end;
121 end;
122 
123 end.

 

posted on 2016-05-31 16:54  疯狂delphi  阅读(9036)  评论(1编辑  收藏  举报

导航