同事插入3个表,要保证全部正确才能提交,有一个插入错误就回滚,把已经插入的表的数据撤销,只用用ADOConnection.BeginTrans事务来实现码?
不用事务只用批量插入能实现多表的回滚吗?

解决方案 »

  1.   

    我想是,至少我是没什么万无一失的没办法了
    http://www.ttjcw.net/html/shujuku/SQLSERVER/983.html
    如何保证数据的一致性和完整性!
      

  2.   

    事务+批量更新模式处理。    
      1、begin tran 
      2、ExecSQL('Insert into Table1 values() '); 
      3、ADOQuery1.LockType := ltBatchOptimistic; 
        SQL条件... 
        ADOQyery1.open; 
        while not ADOQuery1.Eof do 
        begin 
          ADOQuery1.Append; 
          ADOQuery1.fieldbyName('XX').value := 'aa'; 
          ... 
          ...  
          ADOQuery1.Next; 
        end; 
        ADOQuery1.UpdateBatch(arAll); 
      4、commit tran 
     
    在ADOQuery1.Next的前面用ADOQuery1.Post吗?
      

  3.   

    循环中next前可以不用post,因为调用append时,会先提交的,但是在while完毕后,加一句话
      if ADOQuery1.Modified then
        ADOQuery1.Post;
      

  4.   

    try
      BeginTrans;
      ExecSQL('Insert into Table1 values() '); 
      ADOQuery1.LockType := ltBatchOptimistic; 
        SQL条件... 
        ADOQyery1.open; 
        while not ADOQuery1.Eof do 
        begin 
          ADOQuery1.Append; 
          ADOQuery1.fieldbyName('XX').value := 'aa'; 
          ... 
          ... 
          ADOQuery1.post;
          ADOQuery1.Next; 
        end; 
        ADOQuery1.UpdateBatch(arAll); 
      CommitTrans; 
    except
      RollbackTrans;
    end;