WebService相关的问题(关于多层的) (100分)

  • 主题发起人 主题发起人 darkpig
  • 开始时间 开始时间
D

darkpig

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟现在在做一个WebService数据库后台,用WebBroker做前台,因为是第一次做这类的多层的东东,数据的传递不知道该怎么弄好,现在我自己有几种想法,大虾们看能不能帮忙出出主意:)
第一种方法是直接数据以二维数据的形式返回,但考虑到如果是新闻列表或是像有些Grid要用到的数据,从数据量上来考虑好像不大划算,而且这样子考虑到以后如果要再扩展的话就很麻烦,都要按指定的格式来处理。
第二种方法是将数据读出来以后放到ClientDataSet中,然后通过读取其XML文件的形式读取来,可以对文件压缩后再发送,这种方法好像看来会好点,而且之前有位老大也是这样说的,只是现在我对ClientDataSet用法不熟,而且具体操作起来也没底。
看各位大虾们给点意见,我现在的设计都是按第一种方法来的,总是觉得不好,或者看哪位有更好的做法?另外看大虾们能不能给点ClientDataSet的相关使用的代码,最好是从Query中读取出来可以直接放到ClientDataSet中,然后在客户端如何调用的代码,小弟感激不尽:)
 
如果可以解决的话分数没有问题,大虾们帮帮忙吧[:(]
 
学习!!
 
自己解决了。。。sign....
这是我解决的方案,连借用Zlib单元中的压缩功能。
unit UntDataStream;
interface
uses Types, SysUtils,Zlib,Classes,DBClient, ADODB, Provider;
procedure CompressStream(var CompressedStream:TMemoryStream);
function CompressString(SourceString:String):TMemoryStream;
procedure UnCompressStream(var CompressedStream: TMemoryStream);
function UnCompressString(var CompressedStream:TMemoryStream):String;
procedure getClientDataSet(var clDataSet:TClientDataSet;CompressedStream:TMemoryStream);
function getMemoryStreamData(Qry:TAdoQuery):TMemoryStream;
overload;
function getMemoryStreamData(tSp:TADOStoredProc):TMemoryStream;
overload;

implementation

procedure CompressStream(var CompressedStream:TMemoryStream);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: Integer;
begin
Count := CompressedStream.Size;
DestStream := TMemoryStream.Create;
SourceStream:=TCompressionStream.Create(clDefault, DestStream);
Try
CompressedStream.SaveToStream(SourceStream);
SourceStream.Free;

CompressedStream.Clear;
CompressedStream.WriteBuffer(Count, SizeOf(Count));
CompressedStream.CopyFrom(DestStream, 0);
finally
DestStream.Free;
end;
end;

function CompressString(SourceString:String):TMemoryStream;
var
StringStream:TStringStream;
CompressedStream:TMemoryStream;
begin
StringStream:=TStringStream.Create(SourceString);
CompressedStream:=TMemoryStream.Create;
try
CompressedStream.LoadFromStream(StringStream);
CompressStream(CompressedStream);
Result := CompressedStream;
except
Result := NIL;
end;
StringStream.Free;
//CompressedStream.Free;
end;

procedure UnCompressStream(var CompressedStream: TMemoryStream);
var
SourceStream: TDecompressionStream;
DestStream: TMemoryStream;
Buffer: PChar;
Count: Integer;
begin
CompressedStream.Position :=0;
CompressedStream.ReadBuffer(Count, SizeOf(Count));
GetMem(Buffer, Count);
DestStream := TMemoryStream.Create;
SourceStream := TDecompressionStream.Create(CompressedStream);
Try
SourceStream.ReadBuffer(Buffer^, Count);
DestStream.WriteBuffer(Buffer^, Count);
DestStream.Position := 0;//复位流指针
CompressedStream.LoadFromStream(DestStream);
finally
FreeMem(Buffer);
SourceStream.Free;
DestStream.Free;
end;
end;

function UnCompressString(var CompressedStream:TMemoryStream):String;
var
StringStream:TStringStream;
begin
StringStream:=TStringStream.Create('');
try
UnCompressStream(CompressedStream);
StringStream.CopyFrom(CompressedStream,0);
Result := StringStream.DataString;
except
Result := '';
end;
StringStream.Free;
end;

procedure getClientDataSet(var clDataSet:TClientDataSet;CompressedStream:TMemoryStream);
var
tmpStream:TMemoryStream;
begin
tmpStream := TMemoryStream.Create;
try
CompressedStream.Position := 0;
tmpStream.CopyFrom(CompressedStream,0);
tmpStream.Position := 0;
UnCompressStream(tmpStream);
tmpStream.Position := 0;
clDataSet.LoadFromStream(tmpStream);
finally
tmpStream.Free;
end;
end;

function getMemoryStreamData(Qry:TAdoQuery):TMemoryStream;
overload;
var
DataProvider:TDataSetProvider;
clDataSet:TClientDataSet;
CompressedStream: TMemoryStream;
begin
CompressedStream := TMemoryStream.Create;
DataProvider := TDataSetProvider.Create(NIL);
clDataSet := TClientDataSet.Create(NIL);
try
DataProvider.DataSet := Qry;
if not Qry.Active then
Qry.Open;
clDataSet.Data := DataProvider.Data;
clDataSet.SaveToStream(CompressedStream);
CompressedStream.Position := 0;
CompressStream(CompressedStream);
CompressedStream.Position := 0;
Result := CompressedStream;
finally
DataProvider.Free;
clDataSet.Free;
end;

end;

function getMemoryStreamData(tSp:TADOStoredProc):TMemoryStream;
overload;
var
DataProvider:TDataSetProvider;
clDataSet:TClientDataSet;
CompressedStream: TMemoryStream;
begin
CompressedStream := TMemoryStream.Create;
DataProvider := TDataSetProvider.Create(NIL);
clDataSet := TClientDataSet.Create(NIL);
try
DataProvider.DataSet := tSp;
if not tSp.Active then
tSp.Open;
clDataSet.Data := DataProvider.Data;
clDataSet.SaveToStream(CompressedStream);
CompressedStream.Position := 0;
CompressStream(CompressedStream);
CompressedStream.Position := 0;
Result := CompressedStream;
finally
DataProvider.Free;
clDataSet.Free;
end;
end;

end.

这样子可以不结贴吗?
 
后退
顶部