firedac引擎DATASNAP多表查詢和多表提交
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;
firedac引擎DATASNAP多表查詢和多表提交