先看看方法或事件定义:
type TDataRequestEvent = function(Sender: TObject;
Input : OleVariant) : OleVariant of object;
TDataSetProvider的OnDataRequest属性被定义成TDataRequestEvent.
TClientDataSet的方法定义为:
function DataRequest(Data: OleVariant): OleVariant;
virtual;
我们在TDataSetProvider的OnDataRequest事件中编写代码如下:
function TRDM.dspDataRequest(Sender: TObject;
Input : OleVariant):OleVariant;
begin
Result:=VarArrayCreate([0,1],varVariant);
Result[0]:=0;
//0 means no exception
try
//Add your code here
except
on E:Exceptiondo
begin
Result[0]:=-1;//-1 indicate an exception occurred
Result[1]:=E.Message;//return the exception message
end;
end;
end;
客户端
procedure TClientDM.cdsGetCustomData(Data: OleVariant): OleVariant;
begin
Result:=cds.DataRequest(Data);
if not VarIsArray(Result) then
raise Exception.Create('Invalid data');
if Result[0]=-1 then
begin
MessageBox(0,PAnsiChar(Result[1]),'Server Error',MB_OK+MB_ERROR);
VarClear(Result);
end
else
Result:=Result[1];
end;