我用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;
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;
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;
我按你说的只用呀一个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;但我故意把第二个表的值设置成数字型让他出错但发现这个事物并没有起作用呀,第一个表他插入成功了,第二个表他没插入成功但他也不报错,表示回滚事物没执行呀,这是为何!在线等待急
begin
ADOConnection1.RollbackTrans;
Exit;
end;
ADOConnection1.CommitTrans;这里用try没用
不然除非第一句报错,不然ADO不会抛出异常
if ADO_Conn.Errors.Count > 0 then
raise EMyException.Create(ADO_Conn.Errors.Item[0].Description);
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;
执行到这行出错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;