使用App tethering在Delphi XE中在客户端上分页服务器端数据

时间:2017-01-26 17:35:59

标签: delphi delphi-xe8 stringgrid

我有服务器和客户端应用程序。

从服务器端有2个按钮。

第一个按钮"在服务器上显示数据"。 第二个按钮"向客户发送数据"。 在服务器端,我使用FDQuery1SringGrid1TetheringManager1TetheringAppProfile1

从客户端只有1个按钮" Connect"。 在客户端我使用StringGrid1,TetheringManager1和TetheringAppProfile1

首先客户端连接到服务器然后服务器端将数据发送到客户端。

服务器"向客户发送数据"按钮

代码:

procedure TForm1.Button2Click(Sender: TObject);
var rec:integer;
begin
  FDQuery1.SQL.Text := 'SELECT * FROM names';
  FDQuery1.Open;
  rec := FDQuery1.RecordCount;
  FDQuery1.First;

  if rec>0 then
  begin
    while not FDQuery1.Eof do
    begin
      TetheringAppProfile1.Resources.FindByName('Vefa').Value:=FDQuery1.FieldByName('Name').AsString;
      FDQuery1.Next;
    end;
  end;

客户端接收

代码:

procedure TForm2.TetheringAppProfile1Resources1ResourceReceived(
  const Sender: TObject; const AResource: TRemoteResource);
var i:integer;
begin
  for i := 0 to TetheringAppProfile1.Resources.Count do
    StringGrid1.Cells[1,i]:=AResource.Value.AsString;
end;

但是当我从服务器向客户端发送数据时,我看到如下:

IMAGE

1 个答案:

答案 0 :(得分:0)

您可以将名称收集到TStringList中,然后将其作为字符串,使用TStringList.Text属性或作为流发送。

要作为字符串发送(并假设资源名称= NameList),SendDataClick事件处理程序可能如下所示:

procedure TServerForm.btnSendDataClick(Sender: TObject);
var
  sl: TStringList;
begin
  sl := TStringList.Create;
  try
    sl.Add(StringGrid1.Cells[1, 1]);
    sl.Add(StringGrid1.Cells[1, 2]);
    sl.Add(StringGrid1.Cells[1, 3]);
    TetheringAppProfile1.Resources.FindByName('NameList').Value := sl.Text;
  finally
    sl.Free;
  end;
end;

我只是从网格中复制了名称,你可以直接从db记录中完成。

客户OnResourceReceived

procedure TClientForm.TetheringAppProfile1Resources2ResourceReceived(
  const Sender: TObject; const AResource: TRemoteResource);
var
  sl: TStringList;
  i: integer;
begin
  sl := TStringList.Create;
  try
    sl.Text := AResource.Value.AsString;
    for i := 0 to sl.Count-1 do
      StringGrid1.Cells[1, i+1] := sl[i];
  finally
    sl.Free;
  end;
end;

我建议您阅读有关在Malcolm Groves blog

中传递信息流的信息