ADO.NET中使用事务
using (SqlConnection conn = new SqlConnection(k2ConnStr)) { SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlTransaction tran = conn.BeginTransaction(); try { cmd.Transaction = tran; int result = Convert.ToInt32(cmd.ExecuteScalar()); } catch (Exception) { tran.Rollback(); } }