using (var con = new SqlConnection(conString)) { if (con.State != ConnectionState.Open) { con.Open(); } using (SqlTransaction tr = con.BeginTransaction()) { using (SqlCommand cmd = new SqlCommand(sql.ToString(), con)) { cmd.Transaction = tr;//将事务赋值给command就可以了 var result = cmd.ExecuteNonQuery(); if (result > 1) { tr.Commit(); } else { tr.Rollback(); } } } }
原因是cmd没有指定事务