如何在 table 的每一行上 运行 相同的查询? (MVC)

How to run same query on every single row of a table? (MVC)

我目前有一个名为 'Events' 的 table,其中有列 'EquipApprovedDate'、'EquipCalDueDate' 和 'ThemeColor'。我想根据 EquipCalDueDate 之前剩余的天数更新 ThemeColor

绿色 = 好

橙色 = 还可以

红色 = 严重

在日历上注册的每个设备的总天数会有所不同。它将使用此公式计算 (totaldays = EquipCalDueDate - EquipApprovedDate)。剩余天数将使用此公式计算 (remainingDays = EquipCalDueDate - DateTime.Now).

如果 remainingDays 超过 totaldays 的 2/3 它将被标记为 'green'。

如果 remainingDays 小于 totaldays 的 2/3 但超过总天数的 1/3 将被标记为 'orange'。

如果 remainingDays 小于 totaldays 的 1/3 它将被标记为 'red'。

我想在每次加载页面时在 table 上应用整个过程,特别是在数据库中找到的每一行上。基本上是收集数据并 return 每一行。目前只有 运行 部分 ThemeColor 列更新为 'green' 每一行,无论如何.正确的 SQL 查询是什么?

我已将我目前的工作放在一起供您查看。

    con.Open();
        string yyy = "SELECT * FROM [Events]";
        using (SqlCommand cmd = new SqlCommand(yyy, con))
        {
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                String startdate = reader["EquipApprovedDate"].ToString();
                DateTime Sdate = DateTime.Parse(startdate);
                String enddate = reader["EquipCalDueDate"].ToString();
                DateTime Edate = DateTime.Parse(enddate);
                String themecolor = reader["ThemeColor"].ToString();

                double totaldays = (Edate - Sdate).TotalDays;
                double remainingDays = (Edate - DateTime.Now).TotalDays;

                if (remainingDays > (totaldays * (2 / 3)))
                {
                    string sqlCoC = "UPDATE Events SET ThemeColor = 'green'";

                    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MVCConnectionString"].ConnectionString))
                    {
                        SqlCommand coccmd = new SqlCommand(sqlCoC, con);
                        con.Open();
                        coccmd.ExecuteNonQuery();
                        con.Close();
                    }

                    //green = means good
                }
                else if ((remainingDays < (totaldays * (2 / 3))) && (remainingDays > (totaldays * (1 / 3))))
                {

                    string sqlCoC = "UPDATE Events SET ThemeColor = 'orange'";

                    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MVCConnectionString"].ConnectionString))
                    {
                        SqlCommand coccmd = new SqlCommand(sqlCoC, con);
                        con.Open();
                        coccmd.ExecuteNonQuery();
                        con.Close();
                    }

                    //orange = considered okay
                }
                else if (remainingDays < (totaldays * (1 / 3)))
                {

                    string sqlCoC = "UPDATE Events SET ThemeColor = 'red'";

                    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MVCConnectionString"].ConnectionString))
                    {
                        SqlCommand coccmd = new SqlCommand(sqlCoC, con);
                        con.Open();
                        coccmd.ExecuteNonQuery();
                        con.Close();
                    }

                    //red = critical
                }
                else { }
            }
            reader.Close();
            con.Close();
        }

您实际上可以通过一个查询完成整个 table 更新:

declare @remaining int, @total int; update events 
set @remaining=datediff(day, getdate(), equipcalduedate),
@total=datediff(day, equipapproveddate, equipcalduedate),
themecolor=(case
   when @remaining < @total/3 then 'red'
   when @remaining < 2*@total/3 then 'orange'
   else 'green'
end)

对于您提供的示例数据(加上我添加的另一个值来演示 'orange' 主题颜色,输出​​数据来自 select:

select EventId, EquipApprovedDate, EquipCalDueDate, ThemeColor, datediff(day, getdate(), equipcalduedate) as remaining,
datediff(day, equipapproveddate, equipcalduedate) as total
from events

看起来像这样,我想这就是你想要的:

EventId  EquipApprovedDate  EquipCalDueDate  ThemeColor  remaining  total
1        2018-04-17         2018-05-31       green       36         44 
2        2018-04-11         2018-04-27       red         2          16 
3        2020-04-20         2020-05-28       green       764        38 
4        2018-04-11         2018-05-15       orange      20         34 
8        2019-04-20         2019-05-31       green       401        41