【问题标题】:Changing data in table according to date根据日期更改表中的数据
【发布时间】:2021-07-01 12:55:54
【问题描述】:

我有一个包含 3 列 prog_status、opening_date 和 closing_date 的表 Program。我从用户那里检索的开始日期和结束日期值。现在我希望程序状态相对于日期发生变化。因此,如果我当前的系统日期不属于开始和结束日期,则状态应显示字符串值“非活动”,如果它确实属于开始和结束日期范围,则状态应显示为“活动”。此数据需要显示在网格视图中(代码如下):-

<asp:GridView class="table table-bordered table-hover text-nowrap" Border="0" ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
                                                <Columns>
                        <asp:TemplateField HeaderText="Status">  
                <ItemTemplate>  
                    <asp:Label ID="Label1" runat="server" Text='<%#  Bind("prog_status") %>'></asp:Label>  
                </ItemTemplate>  
            </asp:TemplateField>
                        <asp:TemplateField HeaderText="Opening Date">  
                <ItemTemplate>  
                    <asp:Label ID="Label2" runat="server" Text='<%#  Bind("prog_open_date") %>'></asp:Label>  
                </ItemTemplate>  
            </asp:TemplateField>
                        <asp:TemplateField HeaderText="Closing Date">  
                <ItemTemplate>  
                    <asp:Label ID="Label3" runat="server" Text='<%#  Bind("prog_close_date") %>'></asp:Label>  
                </ItemTemplate>  
            </asp:TemplateField>
                        
                         
</Columns>
</asp:GridView>

编辑:- 我在我的 aspx.cs 文件中添加了以下代码,但状态列的值仍然显示为空值。

SqlConnection con = new SqlConnection(//My data source);        

    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd2 = con.CreateCommand();
        cmd2.CommandType = CommandType.Text;
        cmd2.CommandText = SELECT prog_open_date, prog_close_date, prog_status = CASE WHEN GETDATE() >= prog_open_date and GETDATE() <= prog_close_date THEN 'Active' ELSE 'Inactive' END FROM Programs ;
        cmd2.ExecuteNonQuery();
        con.Close();            
    }

【问题讨论】:

    标签: c# sql asp.net


    【解决方案1】:

    如果 prog_status 的值始终取决于系统日期以及 prog_open_date 和 prog_close_date 的值,我觉得 prog_status 应该是数据库中的派生列

    派生列也称为计算列或计算字段。此列不物理存储在数据库中,仅在运行查询时计算。派生列使用其他列的值来生成新字段。

    如果您使用的是 SQL 数据库,则可以使用以下 SQL 语句

    SELECT prog_open_date, prog_close_date, prog_status =   
          CASE          
             WHEN GETDATE() >= prog_open_date and GETDATE() <= prog_close_date THEN 'Active'
             ELSE 'Inactive'  
          END  
    FROM Program 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-11
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      相关资源
      最近更新 更多