如下代码加入事务管理怎么写,??? ( 积分: 50 )

  • 主题发起人 主题发起人 andywos
  • 开始时间 开始时间
A

andywos

Unregistered / Unconfirmed
GUEST, unregistred user!
如下代码加入事务管理怎么写,??????????<br>任何一个出错都要求要回滚<br><br>procedure&nbsp;TForm1.Button4Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;adoquery1.Close;<br>adoquery1.SQL.Clear;<br>adoquery1.SQL.Add('select&nbsp;*&nbsp;from&nbsp;Employees');<br>adoquery1.Open;<br>adoquery1.Append;<br>adoquery1.FieldByName('lastname').AsString:=edit1.Text;<br>adoquery1.FieldByName('firstname').AsString:=edit2.Text;<br>adoquery1.FieldByName('title').AsString:=edit3.Text;<br>adoquery1.FieldByName('titleofcourtesy').AsString:=edit4.Text;<br>adoquery1.Post;<br><br>adoquery1.Close;<br>adoquery1.SQL.Clear;<br>adoquery1.SQL.Add('delete&nbsp;from&nbsp;Employees&nbsp;where&nbsp;lastname='+'''eeee''');<br>adoquery1.ExecSQL;<br><br>adoquery2.Close;<br>adoquery2.SQL.Clear;<br>adoquery2.SQL.Add('select&nbsp;*&nbsp;from&nbsp;AA');<br>adoquery2.Open;<br>adoquery2.Append;<br>adoquery2.FieldByName('AA').AsString:=edit1.Text;<br>adoquery2.FieldByName('BB').AsString:=edit2.Text;<br>adoquery2.FieldByName('CC').AsString:=edit3.Text;<br>adoquery2.FieldByName('DD').AsString:=edit4.Text;<br>adoquery1.Post;<br><br>end;<br>马上给分
 
adoquery1.Connection.BeginTrans;<br>try<br>&nbsp;&nbsp;adoquery1.Close;<br>&nbsp;&nbsp;adoquery1.SQL.Clear;<br>&nbsp;&nbsp;adoquery1.SQL.Add('select&nbsp;*&nbsp;from&nbsp;Employees');<br>&nbsp;&nbsp;adoquery1.Open;<br>&nbsp;&nbsp;adoquery1.Append;<br>&nbsp;&nbsp;adoquery1.FieldByName('lastname').AsString:=edit1.Text;<br>&nbsp;&nbsp;adoquery1.FieldByName('firstname').AsString:=edit2.Text;<br>&nbsp;&nbsp;adoquery1.FieldByName('title').AsString:=edit3.Text;<br>&nbsp;&nbsp;adoquery1.FieldByName('titleofcourtesy').AsString:=edit4.Text;<br>&nbsp;&nbsp;adoquery1.Post;<br><br>&nbsp;&nbsp;adoquery1.Connection.CommitTrans;<br>except<br>&nbsp;&nbsp;adoquery1.Connection.RollbackTrans;<br>end;<br><br>其他的操作相同
 
try<br>&nbsp;adoquery1.BeginTrans;<br>&nbsp;adoquery1.Close;<br>&nbsp;adoquery1.SQL.Clear;<br>&nbsp;adoquery1.SQL.Add('delete&nbsp;from&nbsp;Employees&nbsp;where&nbsp;lastname='+'''eeee''');<br>&nbsp;adoquery1.ExecSQL;<br>&nbsp;adoquery1.CommitTrans;<br>except&nbsp;<br>&nbsp;adoquery1.RollbackTrans;<br>end;
 
to:enigma_UnDead_2006;<br>ADO的事务处理只有TADOConnection有.<br>BDE的事务处理只有TDatabase有.
 
把adoquery1.BeginTrans;<br>adoquery1.CommitTrans;<br>adoquery1.RollbackTrans;<br>这三句话中的adoquery1&nbsp;改成相应的TADOConnection,比如是:ADOConnection1<br>然后你的adoquery1.Connection得指向ADOConnection1&nbsp;就可以了,也就是说不要用adoquery1控件直接去连接数据库.得用TADOConnection连库就行了。
 
我想问一下,<br>如下这样可以?<br>与在ADOQUERY1的BEFOREPOST加入ADOCONNECTION1.BeginTrans;<br>ADOQUERY1的AFTERPOST加入ADOCONNECTION1.CommitTrans;<br>ADOQUERY2的AFTERPOST加入ADOCONNECTION1.CommitTrans;<br>ADOQUERY1的在POSTERROR里加入ADOCONNECTION1.RollbackTrans;<br>ADOQUERY2的在POSTERROR里加入ADOCONNECTION1.RollbackTrans;<br><br>有区别?<br><br><br><br>procedure&nbsp;TForm1.Button4Click(Sender:&nbsp;TObject);<br>begin<br>TRY<br>ADOCONNECTION1.BeginTrans;<br>&nbsp;adoquery1.Close;<br>adoquery1.SQL.Clear;<br>adoquery1.SQL.Add('select&nbsp;*&nbsp;from&nbsp;Employees');<br>adoquery1.Open;<br>adoquery1.Append;<br>adoquery1.FieldByName('lastname').AsString:=edit1.Text;<br>adoquery1.FieldByName('firstname').AsString:=edit2.Text;<br>adoquery1.FieldByName('title').AsString:=edit3.Text;<br>adoquery1.FieldByName('titleofcourtesy').AsString:=edit4.Text;<br>adoquery1.Post;<br><br>adoquery1.Close;<br>adoquery1.SQL.Clear;<br>adoquery1.SQL.Add('delete&nbsp;from&nbsp;Employees&nbsp;where&nbsp;lastname='+'''eeee''');<br>adoquery1.ExecSQL;<br><br>adoquery2.Close;<br>adoquery2.SQL.Clear;<br>adoquery2.SQL.Add('select&nbsp;*&nbsp;from&nbsp;AA');<br>adoquery2.Open;<br>adoquery2.Append;<br>adoquery2.FieldByName('AA').AsString:=edit1.Text;<br>adoquery2.FieldByName('BB').AsString:=edit2.Text;<br>adoquery2.FieldByName('CC').AsString:=edit3.Text;<br>adoquery2.FieldByName('DD').AsString:=edit4.Text;<br>adoquery2.Post;<br>ADOCONNECTION1.CommitTrans;<br>EXCEPT<br>ADOCONNECTION1.RollbackTrans;<br>end;
 
多人接受答案了。
 
后退
顶部