2

我有一个要求,当假期在一周内时,我需要在时间表中禁用字段,我需要禁用当天的字段。但它一直循环到数据库中的假期结束。例如,我将 startdate 设为 11.08.2013,我需要在本周循环到 15.08.2013,因为 8 月 15 日是印度的独立日。

8 月 15 日,该字段应被禁用。但是在这里,我得到了像圣诞节这样的价值,这是一年中的最后一个公共假期,并且整天都受到影响......

代码:

DateTime dtstart = Convert.ToDateTime(startdate);
con.Open();
OleDbCommand cmd1 = new OleDbCommand("select * from ind_holidays", con);
OleDbDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
    string holiday = dr1[0].ToString();
    string date = dr1[1].ToString().Substring(0, 10);
    DateTime holi_date = Convert.ToDateTime(date);
    while (dtstart <= holi_date)
    {
        if (dtstart.DayOfWeek == DayOfWeek.Monday)
        { 
                devmontxt.Text = holiday;
                devmontxt.Enabled = false;
                trmontxt.Text =  holiday;
                trmontxt.Enabled = false;
                lunmontxt.Text = holiday;
                lunmontxt.Enabled = false;
                permontxt.Text = holiday;
                permontxt.Enabled = false;
                totmontxt.Text = holiday;
                totmontxt.Enabled = false;

        }
        if (dtstart.DayOfWeek == DayOfWeek.Tuesday)
        { 

                devtuetxt.Text = holiday;
                devtuetxt.Enabled = false;
                trtuetxt.Text =  holiday;
                trtuetxt.Enabled = false;
                luntuetxt.Text =  holiday;
                luntuetxt.Enabled = false;
                pertuetxt.Text =  holiday;
                pertuetxt.Enabled = false;
                tottuetxt.Text = holiday;
                tottuetxt.Enabled = false;
        }
        if (dtstart.DayOfWeek == DayOfWeek.Wednesday)
        { 

                devwedtxt.Text =  holiday;
                devwedtxt.Enabled = false;
                trwedtxt.Text = holiday;
                trwedtxt.Enabled = false;
                lunwedtxt.Text =  holiday;
                lunwedtxt.Enabled = false;
                perwedtxt.Text =  holiday;
                perwedtxt.Enabled = false;
                totwedtxt.Text =  holiday;
                totwedtxt.Enabled = false;   
        }
        if (dtstart.DayOfWeek == DayOfWeek.Thursday)
        {
            devthutxt.Text = holiday;
            devthutxt.Enabled = false;
            trthutxt.Text = holiday;
            trthutxt.Enabled = false;
            lunthutxt.Text = holiday;
            lunthutxt.Enabled = false;
            perthutxt.Text = holiday;
            perthutxt.Enabled = false;
            totthutxt.Text = holiday;
            totthutxt.Enabled = false;

        }

        if (dtstart.DayOfWeek == DayOfWeek.Friday)
        {
            devfritxt.Text = holiday;
            devfritxt.Enabled = false;
            trfritxt.Text = holiday;
            trfritxt.Enabled = false;
            lunfritxt.Text = holiday;
            lunfritxt.Enabled = false;
            perfritxt.Text = holiday;
            perfritxt.Enabled = false;
            totfritxt.Text = holiday;
            totfritxt.Enabled = false;
        }
        if (dtstart.DayOfWeek == DayOfWeek.Saturday)
        {
                devsattxt.Text =  holiday;
                devsattxt.Enabled = false;
                trsattxt.Text =  holiday;
                trsattxt.Enabled = false;
                lunsattxt.Text =  holiday;
                lunsattxt.Enabled = false;
                persattxt.Text = holiday;
                persattxt.Enabled = false;
                totsattxt.Text =  holiday;
                totsattxt.Enabled = false;    

        }
        dtstart = dtstart.AddDays(1);
        continue;
    }
}
dr1.Close();
con.Close();

}

4

2 回答 2

1

您需要找到一周的最后一天。

lastDay = dtstart.AddDays(7);

现在,由于您有开始日和结束日,因此您只能查询该周的假期。

select * from ind_holidays where holiday_day between @dtstart and @lastDay order by holiday_day

查询返回的第一个元素将是一周中的第一个假期。

另一种选择是获取所有假期并将它们存储在列表中。然后你只需要循环列表中的项目。

于 2013-08-13T14:15:04.567 回答
0

将您的假期保留在列表中,并从现在开始循环一周,并检查列表中是否包含循环中的任何值禁用它。使用当前日期时间并添加您想要的天数。

于 2013-08-13T14:34:43.650 回答