c#开始事物提交事务回滚事务
2018-07-05 11:10 bsdbqling 阅读(1061) 评论(0) 编辑 收藏 举报2 SqlTransaction myTrans = conn.BeginTransaction();//开始事物 13 myCommand.Transaction = myTrans; 14 15 try 16 { 17 myAdapter.Update(ds, strTblName); 18 myTrans.Commit();//提交事务 19 } 20 catch (Exception e) 21 { 22 try 23 { 24 myTrans.Rollback();//回滚并取消数据库的更新 25 } 26 catch (SqlException ex) 27 { 28 if (myTrans.Connection != null) 29 { 30 Console.WriteLine("回滚失败! 异常类型: " + ex.GetType()); 31 } 32 } 33 } 34 finally 35 { 36 conn.Close(); 37 }