zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    MySQL之存储过程和函数
    Mysql中的常用函数:
    Mysql之使用Mysql运算符
    Mysql之单表记录查询
    Mysql之触发器的操作:
    Mysql之视图的操作
    Mysql之表的操作与索引操作
    Mysql之基本操作与数据类型
    Java多线程之生产者消费者
    Java多线程
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/6897116.html
Copyright © 2011-2022 走看看