c# - 允许在 SQL 事务期间读取?

标签 c# asp.net sql-server sql-server-2008

在我的网站上,我有一个事件日历:

enter image description here

现在您会注意到它们被很好地组织成天。

这是管理员用来组织项目的功能。

问题来了

当发生这种情况时,我使用事务范围,因为它会修改多个事件并可能会失败:

例如:

 //needs id '9999'
public string Explode()
{
   string eid = Request.Form["id"];

   using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(2, 45, 0)))
            {
   try
   {
      int id = int.Parse(eid);

      Project p = ProjectManager.GetProject(id);
      int resID = p.Schedule.Employee.EmployeID;
      Employee e = ResourceManager.GetEmployee(resID);
      if (e == null)
         throw new Exception();

      var tasks = ProjectManager.GetProjectTasks(id);
      var ordered = SyncManager.Instance.GetTasks((int)p.PaymoID);

      Task firstTask = null;

      foreach (Paymo.Task t in ordered)
      {
         Task tsk = (from obj in tasks where obj.PaymoID != null && obj.PaymoID.Value == t.ID select obj).FirstOrDefault();
         if (tsk != null && t.UserName == e.EmployeName
            && tsk.Schedule == null)
         {
            if (firstTask == null)
            {
               firstTask = tsk;
            }

            int scheduleID = ScheduleManager.CreateSchedule(p.Schedule.DateFrom.Value
               ,t.BudgetHours <= 0 ? 1.0M : t.BudgetHours , e.EmployeID);

            if (scheduleID == -1)
               throw new Exception();

            if (!ScheduleManager.ChangeTaskSchedule(tsk.TaskID, scheduleID))
               throw new Exception();

            if (!ScheduleManager.GiveLowestPriority(ScheduleManager.GetSchedule(scheduleID)))
            {
               throw new Exception();
            }                           
          }
       }

       ScheduleManager.ChangeProjectSchedule(p.ProjectID, null);
       if (firstTask != null)
       {
          Bump b = new Bump();
          b.DoBump(firstTask.Schedule);
       }
       scope.Complete();
    }

    catch (Exception)
    {
       return "fail";
    }
  } 
  return "";
}

现在的问题是,在交易发生时,如果用户试图以只读模式访问日历,他们必须等待才能看到它。

有没有什么方法可以让网站在交易期间仍然加载? 我使用 LINQ SQL 和 SQL Server 2008 以及 VS 2012。

最佳答案

将您的用途更改为:

new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted,Timeout = new System.TimeSpan(2, 45, 0) }));

关于c# - 允许在 SQL 事务期间读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092069/

相关文章:

c# - 在 ASP.NET View 状态中存储 Dictionary<string, string>?

c# - 下拉列表和数据阅读器

asp.net - 我可以否定使用 "Eval"的属性中的值吗?

sql - 使用 SCOPE_IDENTITY 插入多个表

sql-server - SQL Server 存储过程中的 CASE 语句出现问题

c# - 无法加载文件或程序集 'app_code' 或其依赖项之一

c# - 套接字保持套接字打开,只检查客户端发送的消息

c# - 在 asp.net 代码隐藏页面中添加 <br/> 和 <hr/>

javascript - 使用javascript禁用ascx布局模板中的asp按钮

sql - 将 'LIKE' 与充满字符串的 'IN' 子句一起使用