我用ADOConnection1和ADOQuery2,ADOQuery3 我把ADOQuery2和ADOQuery3都关联到了ADOConnection1,以下是我提交事物的代码,但是出错说这个游标不能在事物中处理这是为何 ADOConnection1.BeginTrans; //创建新事务
 
 try  ADOQuery2.Close;
  ADOQuery2.SQL.Clear;
  ADOQuery2.SQL.Add('INSERT INTO VDCWKKHEAD (VDNO) VALUES('111111');
  ADOQuery2.ExecSQL;  ADOQuery3.Close;
  ADOQuery3.SQL.Clear;
  ADOQuery3.SQL.Add('INSERT INTO VDCWKKHEAD2 (VDNO) VALUES('22222');
  ADOQuery3.ExecSQL;
   ADOConnection1.CommitTrans; //提交事务,事务结束
   except
   ADOConnection1.RollbackTrans; //回滚事务
   end;

解决方案 »

  1.   

    不需要两个ADOQuery的,一个就搞定,还有你代码写错了,少了个):
      ADOQueryTmp.Close;
      ADOQueryTmp.SQL.Clear;
      ADOQueryTmp.SQL.Text:='INSERT INTO VDCWKKHEAD (VDNO) VALUES('111111')';
      ADOQueryTmp.ExecSQL;  ADOQueryTmp.Close;
      ADOQueryTmp.SQL.Clear;
      ADOQueryTmp.SQL.Text:='INSERT INTO VDCWKKHEAD2 (VDNO) VALUES('22222')';
      ADOQueryTmp.ExecSQL;
      

  2.   

    如果再不得就把ADOQueryTmp的lockType设置为ltBatchOptimistic试试
      

  3.   

    to h98458
    我按你说的只用呀一个Adoquery;ADOConnection1.BeginTrans; //创建新事务
      
     try  ADOQuery2.Close;
      ADOQuery2.SQL.Clear;
      ADOQuery2.SQL.Add('INSERT INTO VDCWKKHEAD (VDNO) VALUES('111111');
      ADOQuery2.ExecSQL;  ADOQuery2.Close;
      ADOQuery2.SQL.Clear;
      ADOQuery2.SQL.Add('INSERT INTO VDCWKKHEAD2 (VDNO) VALUES('22222');
      ADOQuery2.ExecSQL;
      ADOConnection1.CommitTrans; //提交事务,事务结束
      except
      ADOConnection1.RollbackTrans; //回滚事务
      end;但我故意把第二个表的值设置成数字型让他出错但发现这个事物并没有起作用呀,第一个表他插入成功了,第二个表他没插入成功但他也不报错,表示回滚事物没执行呀,这是为何!在线等待急
      

  4.   

    SQL语句执行不成功,不会使ExecSQL触发异常,应该象这样判断下:  if ADOQuery1.ExecSQL <= 0 then
      begin
        ADOConnection1.RollbackTrans;
        Exit;
      end;
      ADOConnection1.CommitTrans;这里用try没用
      

  5.   

    多语句事务回滚的话,记得加类似下面这句话。。
    不然除非第一句报错,不然ADO不会抛出异常
        if ADO_Conn.Errors.Count > 0 then
          raise EMyException.Create(ADO_Conn.Errors.Item[0].Description);
      

  6.   

    建议封装一下,我是封装了一个MyExecSql,这样类似的调用,就可以忽略异常抛出的问题。。
    function MyExecSQL(sSQL: string):Integer;
    begin
      with DBMD do
      begin
        if qryExecSQL.Active then
          qryExecSQL.Close;
        qryExecSQL.SQL.Text := sSQL;
        Result := qryExecSQL.ExecSQL;
        if ADO_Conn.Errors.Count > 0 then
          raise EMyException.Create(ADO_Conn.Errors.Item[0].Description);
      end;
    end; 
      

  7.   

    to thx1180
    执行到这行出错if ADOQuery2.ExecSQL <= 0 then 帮看看这是为何,谢谢
    ADOConnection1.BeginTrans; //创建新事务
       
      ADOQuery2.Close;
      ADOQuery2.SQL.Clear;
      ADOQuery2.SQL.Add('INSERT INTO VDCWKKHEAD (VDNO) VALUES('111111');
      ADOQuery2.ExecSQL;  ADOQuery2.Close;
      ADOQuery2.SQL.Clear;
      ADOQuery2.SQL.Add('INSERT INTO VDCWKKHEAD2 (VDNO) VALUES('22222');
      ADOQuery2.ExecSQL;if ADOQuery2.ExecSQL <= 0 then
      begin
      ADOConnection1.RollbackTrans;
      Exit;
      end;
      ADOConnection1.CommitTrans;