如何通过单击日历控件的日期从sql数据库中检索数据 [英] How to retrieve data from sql database by clicking one date of calender control
本文介绍了如何通过单击日历控件的日期从sql数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过单击日历控制的某一日期从sql数据库中检索特定数据.
在我的数据库中,我在表名称记录中使用了名称nvarchar(50),日期nvarchar(50),年份nvarchar(50).
构建成功,但未从数据库检索数据.
I want to retrieve the particular datas from sql database by clicking one date of calender control.
In my database i have taken name nvarchar(50),date nvarchar(50),year nvarchar(50) in the table name record.
Build is succeeded but it is not retrieving the datas from database.
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select *from record where date=''" + Calendar1.SelectedDate.Day.ToString() + "'' and Month=''" + Calendar1.SelectedDate.Year.ToString() + "''", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds, "record");
GridView1.DataSource = ds.Tables["record"].DefaultView;
GridView1.DataBind();
con.Close();
}
先生,请提供解决方案.
Sir please give the solution.
推荐答案
正如所指出的,您正在比较月份和年份,依此类推.还有两个使用数据库的提示:
-在datanase中使用适当的数据类型.不要将日期拆分为varchar
列,而应在表中使用date
类型.
-在查询中使用参数.有关更多信息,请参见: SqlParameter [
As pointed out you''re comparing month against year and so on. Also two hints for using the database:
- use proper datatypes in the datanase. Don''t split the date tovarchar
columns, usedate
type in the table instead.
- use parameters in your queries. For more info see: SqlParameter[^].
SqlCommand cmd =新的SqlCommand(从记录中选择*,其中 date =''" + Calendar1.SelectedDate.Day.ToString() +''和 Month ='' "+ Calendar1.SelectedDate.Year.ToString() +",con);
我认为这永远不会奏效.您正在寻找等于DAY的日期和等于Year的月份.
SqlCommand cmd = new SqlCommand("select *from record where date=''" + Calendar1.SelectedDate.Day.ToString() + "'' and Month=''" + Calendar1.SelectedDate.Year.ToString() + "''", con);
I don''t think it will ever work. You are looking for date which is equal to a DAY and month which is equal to a Year.
这篇关于如何通过单击日历控件的日期从sql数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文