我必须使用SQL Server,WinForms和两个DateTimePickers在一列中填充日期范围。第一个DateTimePicker是范围的开始日期,第二个是该范围的结束日期,并且必须在列中插入所有日期,这些日期包括在第一个DateTimePicker和第二个DateTimePicker中选择的日期之间。
我已经尝试使用下面显示的代码来实现它,但是它不起作用:
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(@"Data Source =.\SQLEXPRESS01; Initial Catalog = Vagtplan; Integrated Security = True"))
{
DateTime startDate = dateTimePicker1.Value;
DateTime finishDate = dateTimePicker2.Value;
DateTime finishDate1 = finishDate.AddDays(+1);
DateTime countDate = dateTimePicker1.Value;
while (!countDate.ToString().Equals(finishDate1.ToString()));
{
if ((countDate.DayOfWeek == DayOfWeek.Monday) | (countDate.DayOfWeek == DayOfWeek.Tuesday)
| (countDate.DayOfWeek == DayOfWeek.Wednesday) | (countDate.DayOfWeek == DayOfWeek.Thursday)
| (countDate.DayOfWeek == DayOfWeek.Friday) | (countDate.DayOfWeek == DayOfWeek.Saturday) | (countDate.DayOfWeek == DayOfWeek.Sunday))
{
SqlCommand command = new SqlCommand("INSERT INTO [Vagter] ([Dato]) VALUES ('" + countDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "')", con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
countDate = countDate.AddDays(1.0);
}
MessageBox.Show("Vagtplanen er oprettet");
}
}
预期的结果是填充列中第一个DateTimePicker中选择的日期和第二个DateTimePicker中选择的日期之间的所有日期。现在没有任何反应。现在显示的错误,什么都没有。
答案 0 :(得分:0)
问题是带while的行末的分号。只需将其删除。
由于其中已包含分号,因此括号内的代码将不会执行,并且循环将重复执行。
替换:
while (!countDate.ToString().Equals(finishDate1.ToString()));
使用:
while (!countDate.ToString().Equals(finishDate1.ToString()))