如何比较 SQLdate 和 CalendarDay
您好,我正在尝试将 Sqldate 与 CalendarDay 进行比较,如果日期匹配,我将更改日历的背景颜色
private void Calendar1_DayRender(Object source, DayRenderEventArgs e)
{
CalendarDay de = null;
int i = 0, j = 0;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB"].ToString()))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@Today", SqlDbType.Date));
cmd.Parameters["@Today"].Value = e.Day.Date.;
cmd.CommandText = " Select Date from tour where date >= @Today";
SqlDataReader sr = cmd.ExecuteReader();
de = (CalendarDay) sr.GetValue(0);
while (sr.NextResult())
{
if (e.Day.Equals(de))
{
e.Cell.BackColor = System.Drawing.Color.Yellow;
}
i++;
de = (CalendarDay)sr.GetValue(i);
}
sr.Close();
sr.Dispose();
}
conn.Close();
}
}
Hi I am trying to compare Sqldate with CalendarDay and if date matches i am changing the bg color of the calendar
private void Calendar1_DayRender(Object source, DayRenderEventArgs e)
{
CalendarDay de = null;
int i = 0, j = 0;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYDB"].ToString()))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@Today", SqlDbType.Date));
cmd.Parameters["@Today"].Value = e.Day.Date.;
cmd.CommandText = " Select Date from tour where date >= @Today";
SqlDataReader sr = cmd.ExecuteReader();
de = (CalendarDay) sr.GetValue(0);
while (sr.NextResult())
{
if (e.Day.Equals(de))
{
e.Cell.BackColor = System.Drawing.Color.Yellow;
}
i++;
de = (CalendarDay)sr.GetValue(i);
}
sr.Close();
sr.Dispose();
}
conn.Close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sql server返回的格式我认为应该是DateTime格式。将 DateTime 与 CalendarDate 进行比较时,应使用 CalendarDay.Date 属性 属性来比较日期。因此,您应该将 e.Day.Date 与从数据库返回的 DateTime.Date 值进行比较。您的数据库应将其值返回到 DateTime 变量中,以便您将 DateTime 与 DateTime 进行比较。
The format returned by sql server I think should be of DateTime format. When you compare a DateTime to a CalendarDate, you should use the CalendarDay.Date Property property to compare the dates. So that for you should be comparing e.Day.Date with a DateTime.Date value returned from the database. Your database should return it's value into a DateTime variable, so you compare a DateTime against a DateTime.