datasnap多层框架在客户端要生成调用远程方法的接口,一个可以鼠标右键点选sqlconnection控件,然后选“generate datasnap client...”,就会自动生成一个所有远程方法的接口单元,这种方法完全不需要我们自己来写代码,超级省事。但也有不好的地方,一个是首先sqlconnection必需要连接上中间件(设置好一些属性后,静态连接),二个是凡中间件有的远程方法一股脑都给你生成了(都让人给看见了)。下面的方法通过自己写代码来声明远程方法调用,避免了前面的两点不足,缺点当然是要自己写代码了。
function TRemoteMethods.GetSearchSQLData(const sSQLStr: string;
clientdataset: TClientDataSet; dbType: Integer = 0): Boolean;
var
v: OleVariant;
begin
Result := False;
if (sSQLStr = '') or (not Assigned(clientdataset)) then
Exit;
SQLConnection1.Connected := False;
try
try
SQLConnection1.Connected := True;
except
on E: Exception do
begin
SysLog.WriteLog('TRemoteMethods.GetSearchSQLData' + E.Message);
Exit;
end;
end;
// 执行
SqlServerMethod1.Close;
SqlServerMethod1.SQLConnection := nil;
SqlServerMethod1.ServerMethodName := '';
SqlServerMethod1.SQLConnection := SQLConnection1;
SqlServerMethod1.ServerMethodName := 'TSysMethods.GetSearchSQLData';
try
SqlServerMethod1.ParamByName('SQLStr').AsString := sSQLStr;
SqlServerMethod1.ParamByName('DBType').AsInteger := dbType;
SqlServerMethod1.ExecuteMethod;
v := Null;
if SqlServerMethod1.ParamByName('ReturnParameter').AsBoolean then
v := SqlServerMethod1.ParamByName('OV').Value;
if not VarIsNull(v) then
begin
clientdataset.Data := v;
Result := True;
end;
except
on E: Exception do
begin
SysLog.WriteLog('TRemoteMethods.GetSearchSQLData' + E.Message);
end;
end;
finally
SqlServerMethod1.Close;
SqlServerMethod1.SQLConnection := nil;
SqlServerMethod1.ServerMethodName := '';
SQLConnection1.Connected := False;
end;
end;