c#事务处理(sqlTransaction)
事务:
/// <summary> /// 删除考勤 /// </summary> /// <param name="dto">Id</param> /// <returns></returns> public ResultEntity<bool> DeleteAttend(DelAttendEditorDTO dto) { //SqlConnection con = new SqlConnection(@"Data Source=.SQLEXPRESS;database=aaaa;uid=sa;pwd=jcx"); //con.Open(); using (var context = new HRModelsContainer()) { var trans = context.Database.BeginTransaction(); try { foreach (var Id in dto.Ids) { //先删从表,再删主表 context.Database.ExecuteSqlCommand("DELETE FROM NoWorkDays WHERE ConfigId = @Id", new SqlParameter("@Id", Id)); context.Database.ExecuteSqlCommand("DELETE FROM AttendancesConfig WHERE Id = @Id", new SqlParameter("@Id", Id)); context.SaveChanges(); } trans.Commit(); } catch (Exception error) { trans.Rollback(); trans.Dispose(); throw error; } finally { trans.Dispose(); } } return GetResultEntity(true); }