firedac引擎DATASNAP多表查询和多表提交

时间:2022-04-29 16:25:02

firedac引擎DATASNAP多表查询和多表提交

属性设置
FDTableAdapter1.SchemaAdapter := FDSchemaAdapter;
FDTableAdapter1.DatSTableName := 't1';
FDMemTable1.Adapter := FDTableAdapter1;
FDTableAdapter2.SchemaAdapter := FDSchemaAdapter;
FDTableAdapter2.DatSTableName := 't2';
FDMemTable2.Adapter := FDTableAdapter2;
FDConection1连接DATASNAP参数设置
FDConection1.DriverName := 'DS';
FDConection1.LoginPrompt := False;
FDStoredProc1.Connection := FDConection1;
FDStoredProc1.StoredProcName := 'TServerMethods.StreamPost';

// 客户端多表提交
procedure TClientForm.PostTables;
var
LMemStream: TMemoryStream;
I: integer;
LDataSet: TDataSet;
begin
for I := 0 to FDSchemaAdapter.Count - 1 do
begin
LDataSet := FDSchemaAdapter.DataSets[I];
if LDataSet <> nil then
if LDataSet.State in dsEditModes then
LDataSet.Post;
end;

LMemStream := TMemoryStream.Create;
try
FDSchemaAdapter.ResourceOptions.StoreItems := [siDelta, siMeta];
FDSchemaAdapter.SaveToStream(LMemStream, TFDStorageFormat.sfBinary);
LMemStream.Position := 0;
FDStoredProc1.Params[0].asStream:= LMemStream;
FDStoredProc1.ExecProc;
except
On E: Exception do
raise Exception.Create(E.Message);
end;
end;
服务端多表提交方法
procedure TServerMethods.StreamPost(AStream: TStream);
var
LMemStream: TMemoryStream;
LErrors: Integer;
begin
// Retreive entire stream from client
LMemStream := CopyStream(AStream);
LMemStream.Position := 0;
try
FDSchemaAdapter.LoadFromStream(LMemStream, TFDStorageFormat.sfBinary);
LErrors := FDSchemaAdapter.ApplyUpdates
finally
LMemStream.Free;
if LErrors > 0 then
raise Exception.CreateFmt(sErrorsOnApplyUpdates , [GenerateErrorMessage]);
end;
end;

服务端多表查询方法
function TServerMethods.StreamGet: TStream;
begin
Result := TMemoryStream.Create;
try
qCustomers.Close;
qCustomers.Open;
qOrders.Close;
qOrders.Open;
FDSchemaAdapter.SaveToStream(Result, TFDStorageFormat.sfBinary);
Result.Position := 0;
except
raise;
end;
end;
客户端多表查询
procedure TClientForm.GetTables;
var
LStringStream: TStringStream;
begin
FDStoredProcGet.ExecProc;
LStringStream := TStringStream.Create(FDStoredProcGet.Params[0].asBlob);
try
if LStringStream <> nil then
begin
LStringStream.Position := 0;
FDSchemaAdapter.LoadFromStream(LStringStream, TFDStorageFormat.sfBinary);
end;
finally
LStringStream.Free;
end;
end;