Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
84 个问题
我不是 IT 专业人士,所以下面的语法是错误的,但它显示了我想做什么。
我有两个 DateTimePicker:DateTimePicker1 和 DateTimePicker2(DateTimePicker1 < DateTimePicker2)。对于这两个 DateTimePicker 之间的任何日期,我想检查:
有人可以帮我清理下面的代码吗?
谢谢!
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
根据您的代码,您需要注意一些问题。 首先,您需要使用形式参数而不是实际参数声明“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
{
}
}
}
如果回复有帮助,请点击“接受答案”并点赞。
注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。