通过Calendar控件指定日期实现数据查询

1、分别添加Calendar控件和GridView控件

clip_image002[1]

2、Calendar控件事件代码:

protected void  Calendar1_SelectionChanged(object sender, EventArgs e) 
    {
        if (this.calendar3.SelectedDate != null)
        {
            string connectiongString = WebConfigurationManager.ConnectionStrings["Database"].ConnectionString;
            SqlConnection con = new SqlConnection(connectiongString);
            con.Open();
            Response.Write("打开数据库成功!");
            Label1.Text = calendar3.SelectedDate.ToShortDateString();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = string.Format("select * from DateDemo where 日期='{0}'", this.calendar3.SelectedDate.ToString());
    //注意参数{0}要用单引号括起来‘{0}’
            SqlDataAdapter adpt = new SqlDataAdapter();
            adpt.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adpt.Fill(ds);
            con.Close();
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
    }

clip_image002[3]

posted @ 2013-06-03 16:53  加油ing  阅读(460)  评论(0编辑  收藏  举报