Z
zgl198171
Unregistered / Unconfirmed
GUEST, unregistred user!
今天看到李维编写的《Delphi.5.x.分布式多层应用系统篇》关于获取Next packet并维护服务器使其成为无状态,编译出现错误不能获取下一个包,特请各位指点。
错误:
project Client.exe raised Exception class EDATABASEErro with message'cdsMaterial:Cannot perform this operation on a closed dataset'.
可是我的cdsMaterial我是打开的啊怎么出现这种错误呢?
下面是源码:
客户端:
procedure TFrmClient.Button3Click(Sender: TObject);//获取下一个包
var
lStart,lEnd:Integer;
abk:TBookMark;
bEof:Boolean;
begin
{获取下一个数据包,是服务器端作为无状态调用,需要ClientDataset BeforeGetRecords
服务器端相应DataProvider的BeforeGetRecords事件支持。}
lStart:=GetTickCount;
Try
Try
While(Not bEof)do
begin
abk:=cdsMaterial.GetBookmark;
if cdsMaterial.GetNextPacket>cdsMaterial.PacketRecords then
bEof:=True;
cdsMaterial.Last;
vOwnerData:=cdsMaterial.fieldByName('fCode').Value;
cdsMaterial.GotoBookmark(abk);
cdsMaterial.Next;
end;
Finally
cdsMaterial.FreeBookmark(abk);
end;
Except
On Exceptiondo
;
end;
lend:=GetTickCount;
lblTime.Caption:=floattostr((lend-lstart)/1000.0)+'秒';
end;
procedure TFrmClient.cdsMaterialBeforeGetRecords(Sender: TObject;
var OwnerData: OleVariant);
begin
if (VarIsEmpty(vOwnerData) or VarIsNull(vOwnerData)) then
vOwnerData:=GetKeyFieldValue(sender);
OwnerData:=vOwnerData;
end;
Function TFrmClient.GetKeyFieldValue(Sender:TObject):OLEVariant;
var
CurrRecord:TBookMark;
begin
Try
With Sender as TClientDatasetdo
begin
CurrRecord:=GetBookMark;
Try
Last;
Result:=FieldByName('fCode').Value;
[red]//错误地方[/red]
GotoBookMark(CurrRecord);
Finally
FreeBookMark(CurrRecord);
end;
end;
Except
On Exception do
;
end;
end;
procedure TFrmClient.FormActivate(Sender: TObject);
begin
dcomconnection1.Connected:=True;
cdsMaterial.Active:=True;
end;
procedure TFrmClient.FormDeactivate(Sender: TObject);
begin
cdsMaterial.Active:=False;
dcomConnection1.Connected:=False;
end;
服务器端事件:
procedure TRDM.dspMaterialBeforeGetRecords(Sender: TObject;
var OwnerData: OleVariant);
begin
with Sender as TDatasetProviderdo
begin
Dataset.Open;
Dataset.Locate('fID',OwnerData,[]);
Dataset.Next;
end;
end;
错误:
project Client.exe raised Exception class EDATABASEErro with message'cdsMaterial:Cannot perform this operation on a closed dataset'.
可是我的cdsMaterial我是打开的啊怎么出现这种错误呢?
下面是源码:
客户端:
procedure TFrmClient.Button3Click(Sender: TObject);//获取下一个包
var
lStart,lEnd:Integer;
abk:TBookMark;
bEof:Boolean;
begin
{获取下一个数据包,是服务器端作为无状态调用,需要ClientDataset BeforeGetRecords
服务器端相应DataProvider的BeforeGetRecords事件支持。}
lStart:=GetTickCount;
Try
Try
While(Not bEof)do
begin
abk:=cdsMaterial.GetBookmark;
if cdsMaterial.GetNextPacket>cdsMaterial.PacketRecords then
bEof:=True;
cdsMaterial.Last;
vOwnerData:=cdsMaterial.fieldByName('fCode').Value;
cdsMaterial.GotoBookmark(abk);
cdsMaterial.Next;
end;
Finally
cdsMaterial.FreeBookmark(abk);
end;
Except
On Exceptiondo
;
end;
lend:=GetTickCount;
lblTime.Caption:=floattostr((lend-lstart)/1000.0)+'秒';
end;
procedure TFrmClient.cdsMaterialBeforeGetRecords(Sender: TObject;
var OwnerData: OleVariant);
begin
if (VarIsEmpty(vOwnerData) or VarIsNull(vOwnerData)) then
vOwnerData:=GetKeyFieldValue(sender);
OwnerData:=vOwnerData;
end;
Function TFrmClient.GetKeyFieldValue(Sender:TObject):OLEVariant;
var
CurrRecord:TBookMark;
begin
Try
With Sender as TClientDatasetdo
begin
CurrRecord:=GetBookMark;
Try
Last;
Result:=FieldByName('fCode').Value;
[red]//错误地方[/red]
GotoBookMark(CurrRecord);
Finally
FreeBookMark(CurrRecord);
end;
end;
Except
On Exception do
;
end;
end;
procedure TFrmClient.FormActivate(Sender: TObject);
begin
dcomconnection1.Connected:=True;
cdsMaterial.Active:=True;
end;
procedure TFrmClient.FormDeactivate(Sender: TObject);
begin
cdsMaterial.Active:=False;
dcomConnection1.Connected:=False;
end;
服务器端事件:
procedure TRDM.dspMaterialBeforeGetRecords(Sender: TObject;
var OwnerData: OleVariant);
begin
with Sender as TDatasetProviderdo
begin
Dataset.Open;
Dataset.Locate('fID',OwnerData,[]);
Dataset.Next;
end;
end;