C
cnhotel
Unregistered / Unconfirmed
GUEST, unregistred user!
服务端有多条记录,我想一次性传回给客户端!有没有其它什么方法也可以实现呢?
服务端:
procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var cmd:String;
DList:TStringList;
Stream1:TMemoryStream;
begin
cmd:=Athread.Connection.ReadLn;
if cmd='testdata' then
begin
//获取数据,肯定有记录
DList:=TStringList.Create;
Stream1:= TMemoryStream.Create;
ADOQuery1.SQL.Text:='select top 3 uname from usertable';
ADOQuery1.Open;
while not ADOquery1.Eof do
begin
Dlist.Add(ADOQuery1.fieldbyname('jldwmc').AsString);
ADOQuery1.Next;
end;
Dlist.SaveToStream(Stream1);
Stream1.Position := 0;
AThread.Connection.WriteStream(Stream1);
end;
end;
客户端:
procedure TfrmMain.Button3Click(Sender: TObject);
var Stream1:TMemoryStream;
DList:TStringList;
i:Integer
begin
IdTCPClient.WriteLn('testdata');
Stream1:=TMemoryStream.Create;
DList:=TStringList.Create;
i:=Idtcpclient.ReadFromStack(True,-2,True);
if i>0 then
begin
IdTCPClient.ReadStream(Stream1,i);
Stream1.Position:=0;
Dlist.LoadFromStream(Stream1);
if Dlist.Count>0 then //值为0,没有接收到服务器返回的值了,为什么?
begin
ShowMessage('ok');
end else ShowMessage('null');
end;
end;
服务端:
procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var cmd:String;
DList:TStringList;
Stream1:TMemoryStream;
begin
cmd:=Athread.Connection.ReadLn;
if cmd='testdata' then
begin
//获取数据,肯定有记录
DList:=TStringList.Create;
Stream1:= TMemoryStream.Create;
ADOQuery1.SQL.Text:='select top 3 uname from usertable';
ADOQuery1.Open;
while not ADOquery1.Eof do
begin
Dlist.Add(ADOQuery1.fieldbyname('jldwmc').AsString);
ADOQuery1.Next;
end;
Dlist.SaveToStream(Stream1);
Stream1.Position := 0;
AThread.Connection.WriteStream(Stream1);
end;
end;
客户端:
procedure TfrmMain.Button3Click(Sender: TObject);
var Stream1:TMemoryStream;
DList:TStringList;
i:Integer
begin
IdTCPClient.WriteLn('testdata');
Stream1:=TMemoryStream.Create;
DList:=TStringList.Create;
i:=Idtcpclient.ReadFromStack(True,-2,True);
if i>0 then
begin
IdTCPClient.ReadStream(Stream1,i);
Stream1.Position:=0;
Dlist.LoadFromStream(Stream1);
if Dlist.Count>0 then //值为0,没有接收到服务器返回的值了,为什么?
begin
ShowMessage('ok');
end else ShowMessage('null');
end;
end;