这是我的场景:
- 给定
startDate = 1/7/2012
和endDate = 9/7/2012
- 数据库包含:
4/7/2012
和5/7/2012
- 读取并返回不包含在数据库中的时间间隔的日期
预期结果(日/月/年):
1/7/2012
2/7/2012
3/7/2012
6/7/2012
7/7/2012
8/7/2012
9/7/2012
以下是我正在使用的代码:
DateTime startDate = new DateTime(2012, 7, 1, 0, 0, 0);
DateTime endDate = new DateTime(2012, 7, 9, 0, 0, 0);
int interval = 1;
MySqlConnection connDate = new MySqlConnection(connectionString);
MySqlCommand cmdDate = connDate.CreateCommand();
cmdDate.Parameters.Add("username", MySqlDbType.VarChar);
cmdDate.Parameters["username"].Value = Session["username"].ToString();
cmdDate.CommandText = "SELECT * FROM report WHERE username = @username";
connDate.Open();
MySqlDataReader drDate = cmdDate.ExecuteReader();
this.Label1.Text = "";
for (DateTime dateTime = startDate; dateTime < endDate; dateTime += TimeSpan.FromDays(interval))
{
//start from here, i don't know exactly what i am doing.
DateTime reportDate = Convert.ToDateTime(null);
while (drDate.Read())
{
reportDate = Convert.ToDateTime(drDate["reportDate"].ToString());
}
if (Convert.ToDateTime(drDate["reportDate"].ToString()).ToShortDateString() != reportDate.ToShortDateString())
{
this.Label1.Text += dateTime.ToString() + "</br>";
}
}
connDate.Close();
问题是,上面的代码显示了 2012 年 7 月 1 日至 9 日的所有日期。相反,我希望它显示除数据库中已包含的 7 月 4 日和 5 日之外的所有日期。