如何比较SQLdate和CalendarDay

时间:2011-04-12 23:08:52

标签: c# asp.net-ajax calendar

您好我正在尝试将Sqldate与CalendarDay进行比较,如果日期匹配,我正在更改日历的bg颜色

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();
    }
}

1 个答案:

答案 0 :(得分:0)

我认为sql server返回的格式应该是DateTime格式。将DateTime与CalendarDate进行比较时,应使用CalendarDay.Date Property属性来比较日期。因此,您应该将e.Day.Date与从数据库返回的DateTime.Date值进行比较。您的数据库应将其值返回到DateTime变量中,因此您将DateTime与DateTime进行比较。