C# for 循环:检查 DateTimePicker 是周末还是在数据库中

Jiale Xue - MSFT 34,276 信誉分 Microsoft 供应商
2024-04-15T05:53:18.3+00:00

我不是 IT 专业人士,所以下面的语法是错误的,但它显示了我想做什么。

我有两个 DateTimePicker:DateTimePicker1 和 DateTimePicker2(DateTimePicker1 < DateTimePicker2)。对于这两个 DateTimePicker 之间的任何日期,我想检查:

  1. 如果是周末。
  2. 如果它是数据库表中的日期。

有人可以帮我清理下面的代码吗?

谢谢!

public static bool isDateInDatabaseAppointmentTable(DateTime DateTimePicker.Value)  //How to write this statement. Check if the date is in database table
{

    OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString);


    string select = "SELECT COUNT(*) from TableAppointment WHERE AppointmentDate = DataTimePicker.Value ";
    //How to write this SQL statement

    using (OdbcCommand cmd = new OdbcCommand(select, Cn))            
    {
        object obj = cmd.ExecuteScalar();

        int count = Convert.ToInt32(obj);

        if (count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }                          

    }


}



for (DateTime dt = DateTimePicker1.Value to DateTimePicker2.Value) 
{
    bool isFound = GlobalMethod.isDateInDatabaseAppointmentTable(dt);

    if (dt == Satursday || dt == Sunday)
    {
    MessageBox.Show("It is weekend, you don't work today")
    //I will do something here, and I think I know how to do it. Just using messagebox to replace it.
    }  
    else if (isFound == true)   
    {
    MessageBox.Show("You have appointment today, and you don't work today")
    //I will do something here, and I think I know how to do it. Just using messagebox to replace it.
    }  
    else
    {
    //I will do something here.
    }


}

Note:此问题总结整理于: C# for loop: Checking if DateTimePicker is weekend or in database

Windows 窗体
Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
84 个问题
C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
112 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 40,786 信誉分 Microsoft 供应商
    2024-04-15T09:16:54.5733333+00:00

    根据您的代码,您需要注意一些问题。 首先,您需要使用形式参数而不是实际参数声明“isDateInDatabaseAppointmentTable”方法。 然后你就不能在 For 循环中使用 to 运算符。 最后,您可以使用 cheong00 所说的 DateTime.DayOfWeek 查看星期几。 以下是您可以参考的修改代码。

    public static bool isDateInDatabaseAppointmentTable(DateTime dt)   
    {  
       ....  
    }  
      
    private void button1_Click(object sender, EventArgs e)  
    {  
        var startDate = dateTimePicker1.Value;  
        var endDate = dateTimePicker2.Value;  
        for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))  
        {  
            bool isFound = isDateInDatabaseAppointmentTable(date);  
      
            if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)  
            {  
                MessageBox.Show("It is weekend, you don't work today");  
            }  
            else if (isFound == true)  
            {  
                MessageBox.Show("You have appointment today, and you don't work today");  
      
            }  
            else  
            {  
            }  
        }  
    }  
    

    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助