DataSnap——利用TParams进行多表事务更新

Posted 咏南中间件和开发框架

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataSnap——利用TParams进行多表事务更新相关的知识,希望对你有一定的参考价值。

DataSnap——利用TParams进行多表事务更新

服务端:

function TSVRDM.multUpdatesByPar(UpdateParam: TParams; out ErrMsg: string): OleVariant;
const
aSQL = ‘Select * from %s where 1<>1‘;
var
i: Integer;
lQuery: TADOQuery;
lProvider: TDataSetProvider;
conn: TADOConnection;
ErrorCount, MaxErrors: Integer;
begin
Writeln(fguid + ‘:Mult-Table Update start ...‘);
conn := ConnPool.Lock(dbConnStr);
lQuery := TADOQuery.Create(NIL);
lProvider := TDataSetProvider.Create(nil);
Writeln(fguid + ‘: Start Transaction... ‘ );
conn.BeginTrans;
try
try
lQuery.Connection := conn;
lProvider.DataSet := lQuery;

for I := 0 to UpdateParam.Count-1 do begin
Writeln(fguid + ‘: Update Table ‘ + UpdateParam[i].Name);

lQuery.Close;
lQuery.SQL.Clear;
lQuery.SQL.Text := Format(aSQL, [UpdateParam[i].Name]);

result := lProvider.ApplyUpdates(UpdateParam[i].AsBytes, 0, ErrorCount);
ErrMsg := FErrMsg;
if ErrorCount > 0 then
raise Exception.Create(ErrMsg + ‘; TableName = ‘+UpdateParam[i].Name);
end;
conn.CommitTrans;
Writeln(fguid + ‘: Transaction commited... Update finished!‘ );
except
on E: Exception do
begin
conn.RollbackTrans;
Writeln(fguid + ‘:Transaction rollbacked! Update ERROR :‘ + E.Message);
end;
end;
finally
lProvider.Free;
lQuery.Free;
ConnPool.Unlock(conn);
FErrMsg := ‘‘;
end;

end;

客户端:

procedure TMyClient.btn1Click(Sender: TObject);
var
aParams: TParams;
aPar: TParam;
client: TDMClient;
ErrMsg: string;
begin

// TClientDataSet的Data和Delta可用TParam.AsBytes直接传递
aParams := TParams.Create(nil);
if ClientDataSet1.ChangeCount > 0 then
begin
aPar := aParams.CreateParam(ftVarBytes, ‘数据表1‘, ptInput);
aPar.AsBytes := ClientDataSet1.Delta;
end;

if ClientDataSet2.ChangeCount > 0 then
begin
aPar := aParams.CreateParam(ftVarBytes, ‘数据表2‘, ptInput);
aPar.AsBytes := ClientDataSet2.Delta;
end;
if aParams.Count = 0 then
exit;

client := TDMClient.Create(self.SQLConn.DBXConnection);
try
try
client.multUpdatesByPar(aParams, ErrMsg);
if ErrMsg <> ‘‘ then
raise Exception.Create(ErrMsg)
else
begin
ClientDataSet1.MergeChangeLog;
ClientDataSet2.MergeChangeLog;
end;
except
ON E: Exception do
begin
showmessage(E.Message);
end;
end;
finally
client.Free;

end;
end;

 

以上是关于DataSnap——利用TParams进行多表事务更新的主要内容,如果未能解决你的问题,请参考以下文章

delphixe android mssql services datasnap 哪个好

分区取模分库分表策略:多表事务分库内闭环解决方案

利用EF Core的Lambda表达式和Join进行多表查询

利用EF Core的Lambda表达式和Join进行多表查询

SQL多表&事务

cassandra中多表ACID事务的解决方法是啥