首页 > 数据库技术 > 详细

c#事务处理(sqlTransaction)

时间:2018-09-10 12:34:01      阅读:223      评论:0      收藏:0      [点我收藏+]

事务:

 /// <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);
    }

 

c#事务处理(sqlTransaction)

原文:https://www.cnblogs.com/likui-bookHouse/p/9618332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!