詠南中介軟體(datasnap)新介面演示
阿新 • • 發佈:2020-12-21
詠南中介軟體(datasnap)新介面演示
/// <author>cxg 2020-12-19</author> /// datasnap http客戶端介面呼叫演示 unit Unit2; interface uses untGlobal, ClientClassesUnit1, MsgPack, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ToolWin, Vcl.ComCtrls, Data.DB, Datasnap.DBClient, Vcl.Grids, Vcl.DBGrids, Data.DbxDatasnap, Data.DBXCommon, Data.DbxHTTPLayer, Datasnap.DSClientRest; type TForm2 = class(TForm) ToolBar1: TToolBar; ToolButton1: TToolButton; DBGrid1: TDBGrid; DBGrid2: TDBGrid; ClientDataSet1: TClientDataSet; DataSource1: TDataSource; ClientDataSet2: TClientDataSet; DataSource2: TDataSource; DSRestConnection1: TDSRestConnection; ToolButton2: TToolButton; ToolButton3: TToolButton; ToolButton4: TToolButton; ToolButton5: TToolButton; ToolButton6: TToolButton; procedure ToolButton1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ToolButton2Click(Sender: TObject); procedure ToolButton3Click(Sender: TObject); procedure ToolButton4Click(Sender: TObject); procedure ToolButton5Click(Sender: TObject); procedure ToolButton6Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} procedure TForm2.FormCreate(Sender: TObject); begin DSRestConnection1.Host := '127.0.0.1'; //中介軟體ip and port DSRestConnection1.Port := 9000; DSRestConnection1.Context := 'yn/'; end; procedure TForm2.ToolButton1Click(Sender: TObject); //查詢 var m: Tmethod1Client; send, recv: TMsgPack; begin send := TMsgPack.Create; m := Tmethod1Client.Create(DSRestConnection1); send.Force('cmd').AsString := 'query'; //命令字 send.Force('accountno').AsString := '1'; //資料庫帳套號 send.Force('tablenum').AsInteger := 2; //查詢幾個表? send.Force('sql1').AsString := 'select top 100 * from tunit'; send.Force('sql2').AsString := 'select * from tgoods'; recv := m.remoteMethod(send); if recv.Force('result').AsBoolean then begin ClientDataSet1.Data := DeCompressData(recv.Force('dataset1').AsVariant); //unzip ClientDataSet2.Data := DeCompressData(recv.Force('dataset2').AsVariant); //unzip end; m.Free; end; procedure TForm2.ToolButton2Click(Sender: TObject); //儲存 var m: Tmethod1Client; send, recv: TMsgPack; begin if ClientDataSet1.State in dsEditModes then ClientDataSet1.Post; if ClientDataSet2.State in dsEditModes then ClientDataSet2.Post; if ClientDataSet1.ChangeCount = 0 then Exit; if ClientDataSet2.ChangeCount = 0 then Exit; send := TMsgPack.Create; m := Tmethod1Client.Create(DSRestConnection1); send.Force('cmd').AsString := 'save'; //命令字 send.Force('accountno').AsString := '1'; //資料庫帳套號 send.Force('tablenum').AsInteger := 2; send.Force('tablename1').AsString := 'tunit'; send.Force('tablename2').AsString := 'tgoods'; send.Force('delta1').AsVariant := ClientDataSet1.Delta; send.Force('delta2').AsVariant := ClientDataSet2.Delta; recv := m.remoteMethod(send); if recv.Force('result').AsBoolean then begin ClientDataSet1.MergeChangeLog; ClientDataSet2.MergeChangeLog; end else begin ClientDataSet1.Cancel; ClientDataSet2.Cancel; end; m.Free; end; procedure TForm2.ToolButton3Click(Sender: TObject); //儲存過程 var m: Tmethod1Client; send, recv: TMsgPack; begin send := TMsgPack.Create; m := Tmethod1Client.Create(DSRestConnection1); send.Force('cmd').AsString := 'spopen';//命令字 send.Force('accountno').AsString := '1';//資料庫帳套號 send.Force('spname').AsString := '儲存過程名'; send.Force('params').AsString := '@引數1=1;@引數2=2'; recv := m.remoteMethod(send); if recv.Force('result').AsBoolean then begin ClientDataSet1.Data := DeCompressData(recv.Force('dataset1').AsVariant); //unzip end; m.Free; end; procedure TForm2.ToolButton4Click(Sender: TObject); //execsql var m: Tmethod1Client; send, recv: TMsgPack; begin send := TMsgPack.Create; m := Tmethod1Client.Create(DSRestConnection1); send.Force('cmd').AsString := 'execsql';//命令字 send.Force('accountno').AsString := '1';//資料庫帳套號 send.Force('sql').AsString := 'insert into tunit(unitid,unitname) values (''1'', ''2'')'; recv := m.remoteMethod(send); if recv.Force('result').AsBoolean then begin ShowMessage('execsql ok'); end; m.Free; end; procedure TForm2.ToolButton5Click(Sender: TObject); //downfile var m: Tmethod1Client; send, recv: TMsgPack; begin send := TMsgPack.Create; m := Tmethod1Client.Create(DSRestConnection1); send.Force('cmd').AsString := 'downfile';//命令字 send.Force('filenum').AsInteger := 2;//下載幾個檔案? send.Force('filename1').AsString := 'filename1'; send.Force('filename2').AsString := 'filename2'; recv := m.remoteMethod(send); if recv.Force('result').AsBoolean then begin recv.Force('file1').SaveBinaryToFile('filename1'); recv.Force('file2').SaveBinaryToFile('filename2'); end else ShowMessage('downfile fail'); m.Free; end; procedure TForm2.ToolButton6Click(Sender: TObject); //upfile var m: Tmethod1Client; send, recv: TMsgPack; begin send := TMsgPack.Create; m := Tmethod1Client.Create(DSRestConnection1); send.Force('cmd').AsString := 'upfile';//命令字 send.Force('filenum').AsInteger := 2;//上傳幾個檔案? send.Force('filename1').AsString := 'filename1'; send.Force('filename2').AsString := 'filename2'; send.Force('file1').LoadBinaryFromFile('filename1'); send.Force('file2').LoadBinaryFromFile('filename2'); recv := m.remoteMethod(send); if recv.Force('result').AsBoolean then begin ShowMessage('upfile ok'); end else ShowMessage('upfile fail'); m.Free; end; end.