在mysql表中插入日期时阻止所有周三和周四

kulphzqa  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(193)

通过下面的代码,我可以在mysql表中插入一系列日期,但是我需要基于工作日限制日期。例如,我想限制所有进入表中的日期,即星期三。

protected void Submit_click(object sender, EventArgs e)
{
DateTime startdate = Convert.ToDateTime(txtstartdate.Text);
DateTime enddate = Convert.ToDateTime(txtenddate.Text);
for (DateTime date = startdate; date <= enddate; date = date.AddDays(1))
{
    try
    {
        var shtdate = date.ToShortDateString();
        string MyConString = "SERVER=localhost;DATABASE=mydb;UID=myid;PASSWORD=abc123;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        string cmdText = "INSERT INTO approved(agentlogin ,leavetype ,date ,time, reason)VALUES ( @login, @type, @date, 'Full day', @reason)";
        MySqlCommand cmd = new MySqlCommand(cmdText, connection);
        cmd.Parameters.AddWithValue("@login", Label1.Text);
        cmd.Parameters.AddWithValue("@type", ddlleavetype.Text);
        cmd.Parameters.AddWithValue("@date", shtdate);
        cmd.Parameters.AddWithValue("@reason", txtreason.Text);

        connection.Open();

        int result = cmd.ExecuteNonQuery();
        connection.Close();

        //lblError.Text = "Data Saved";

    }
    catch (Exception)
    {
        Console.Write("not entered");
        //lblError.Text = ex.Message;
    }
}
}

那我该怎么办呢?谢谢你的帮助

moiiocjp

moiiocjp1#

你可以试试 DateTime.DayOfWeek 跳过所有已经过的日期 DayOfWeek.Wednesday 或者 DayOfWeek.Thursday 这样地:

for (DateTime date = startdate; date <= enddate; date = date.AddDays(1))
{
    if (date.DayOfWeek == DayOfWeek.Wednesday || date.DayOfWeek == DayOfWeek.Thursday)
    {
        // skip to next day
        continue;
    }

    try 
    {
        // write to database
    }
    catch (Exception)
    {
        Console.Write("not entered");
        //lblError.Text = ex.Message;
    }
}
1mrurvl1

1mrurvl12#

datetime具有dayofweek属性(看看这里)。
你可以在if语句中使用它,你就可以开始了。

rekjcdws

rekjcdws3#

我没有根据星期三和星期四这两个工作日制定条件,而是根据其他5天编写了条件,如下所示。

protected void Submit_click(object sender, EventArgs e)
{
    DateTime startdate = Convert.ToDateTime(txtstartdate.Text);
    DateTime enddate = Convert.ToDateTime(txtenddate.Text);
    for (DateTime date = startdate; date <= enddate; date = date.AddDays(1))
    {
         if (date.DayOfWeek == DayOfWeek.Monday || date.DayOfWeek == DayOfWeek.Tuesday || date.DayOfWeek == DayOfWeek.Friday || date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
         {
              var shtdate = date.ToShortDateString();
              string MyConString = "SERVER=localhost;DATABASE=mydb;UID=myid;PASSWORD=abc123;";
              MySqlConnection connection = new MySqlConnection(MyConString);
              string cmdText = "INSERT INTO approved(agentlogin ,leavetype ,date ,time, reason)VALUES ( @login, @type, @date, 'Full day', @reason)";
              MySqlCommand cmd = new MySqlCommand(cmdText, connection);
              cmd.Parameters.AddWithValue("@login", Label1.Text);
              cmd.Parameters.AddWithValue("@type", ddlleavetype.Text);
              cmd.Parameters.AddWithValue("@date", shtdate);
              cmd.Parameters.AddWithValue("@reason", txtreason.Text);
              connection.Open();
              int result = cmd.ExecuteNonQuery();
         }
         else{ 
         }
    }

}

相关问题