如何比较 SQLdate 和 CalendarDay

发布于 2024-11-01 15:38:20 字数 1172 浏览 0 评论 0原文

您好,我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤凫 2024-11-08 15:38:20

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文