private static void Execute(string connectionString) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlTransaction transaction; using (SqlCommand cmd = connection.CreateCommand()) { //启动事务 transaction = connection.BeginTransaction(); cmd.Connection = connection; cmd.Transaction = transaction; try { cmd.CommandText = "sql语句!"; cmd.ExecuteNonQuery(); //完成提交 transaction.Commit(); } catch(Exception ex) { //数据回滚 transaction.Rollback(); throw ex; } } } }
在ADO.NET中使用SqlTransaction事务的例子代码,需要说明的已经写成注释。参考:http://www.cnblogs.com/wang726zq/archive/2012/05/17/Transaction.html
时间: 2024-10-04 22:10:08