【问题标题】:How to run same query on every single row of a table? (MVC)如何在表的每一行上运行相同的查询? (MVC)
【发布时间】:2018-04-25 01:37:02
【问题描述】:

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

绿色 = 好

橙色 = 认为还可以

红色 = 严重

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

如果 remainingDays 超过 totaldays 的 2/3,它将被标记为“绿色”。

如果 remainingDays 小于 totaldays 的 2/3 但超过 totaldays 的 1/3将被标记为“橙色”。

如果 remainingDays 小于 totaldays 的 1/3,它将被标记为“红色”。

我想在每次加载页面时在表上应用整个过程,特别是在数据库中找到的每一行。基本上是收集数据并为每一行返回它。目前,它仅在 ThemeColor 列更新为每行的“绿色”的部分运行,无论如何。什么是正确的 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();
        }

【问题讨论】:

  • EquipCalDueDateEquipApprovedDate 使用什么数据类型?如果是字符类型,那么日期的格式是什么?
  • 在表格中都设置为“日期”。
  • 我刚刚更新了我的问题,附有表格数据和为您设计的视图。

标签: sql mdf database-table local-database


【解决方案1】:

您实际上可以通过一个查询来更新整个表:

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)

对于您提供的示例数据(加上我为演示“橙色”主题颜色添加的另一个值,来自选择的输出数据:

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 

【讨论】:

  • 会尝试并回复你尼克。
  • 老兄,你真是天才!它就像魅力一样。由于我正在处理 MVC 项目,我只是将上面的查询存储在存储过程中,然后在页面加载时调用它。非常感谢!
  • 谢谢!很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2016-12-07
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多