• 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);
        }
  • 相关阅读:
    mybatis-config.xml详解
    过滤器与拦截器
    Tomcat 部署web 项目
    Tomcat架构
    git stash
    AbstractQueuedSynchronizer 源码解读(转载)
    Kafka 转载
    Oracle数据库TNS详解
    Oracle建表知识全面详解
    Oracle高级教程
  • 原文地址:https://www.cnblogs.com/likui-bookHouse/p/9618332.html
Copyright © 2020-2023  润新知