[英]Searching Gridview by Date, but the Calendar control picker (that is DateTime) includes the date and also the time, example: 2013-01-01 00:00:00.000
I have a GridView that should be populated according to a selected date, so I made the use of the Calendar control
. 我有一个GridView,应该根据选择的日期进行填充,因此我使用了
Calendar control
。
The problem 问题
Calendar
seems to be is in the format : dd-MM-yyy
Calendar
似乎是格式为 : dd-MM-yyy
date field
in the Database is of type datetime
meaning : dd-MM-yyyy HH:mm:SS.ms
date field
的类型为datetime
含义是 : dd-MM-yyyy HH:mm:SS.ms
So there will be no result since the Calendar control and the Database field are of different types. 因此,由于Calendar控件和Database字段的类型不同,因此不会有结果。
What i tried 我尝试了什么
SqlDataSource1.SelectCommand = "Select * from table1 where start_date Like '% " & calendar1.SelectedDate.Date & " %' "
As you can see i worked the sqldatasource in code behind and i made the use of Like
But even with the use of Like
no results show due to Database having not only date but time too . 如您所见,我在后面的代码中使用了sqldatasource,并且我使用了
Like
但是即使使用Like
由于数据库不仅具有日期而且具有时间,因此未显示任何结果。
I was also thinking to change the sql query itself, and precisely the field start_date
, to omit the time (HH:mm:SS.ms) but is it possible to do so? 我也在考虑更改sql查询本身,也就是更改字段
start_date
,以忽略时间(HH:mm:SS.ms),但是可以这样做吗?
Even if the Calendar Control's selected date and the field in the database are both DateTime, the search won't work because when searching with the Calendar Control its query will be Date and Time example 2013-01-01 00:00:00.000
while in the DB the time is not always 00:00:00 thus the need of Between as the answer below. 即使Calendar Control的选定日期和数据库中的字段均为DateTime,搜索也将无法进行,因为当使用Calendar Control搜索时,其查询将是Date and Time示例
2013-01-01 00:00:00.000
DB的时间并不总是00:00:00,因此下面的答案之间需要。
Calendar.SelectedDate
is DateTime
. Calendar.SelectedDate
是DateTime
。 So just change calendar1.SelectedDate.Date
to calendar1.SelectedDate
. 因此,只需将
calendar1.SelectedDate.Date
更改为calendar1.SelectedDate
。
Proof: http://msdn.microsoft.com/ru-ru/library/system.web.ui.webcontrols.calendar.selecteddate.aspx 证明: http : //msdn.microsoft.com/ru-ru/library/system.web.ui.webcontrols.calendar.selecteddate.aspx
You can use a Date to extract records from a database using a DateTime field. 您可以使用Date通过DateTime字段从数据库中提取记录。 I dont think using LIKE on dates is a good idea.
我不认为在日期上使用LIKE是个好主意。 Specify From and To date ranges to get one day worth of data:
指定“从”和“到”日期范围以获取一天的数据价值:
SqlDataSource1.SelectCommand = "Select * from table1 where start_date >" & calendar1.SelectedDate.Date & " AND start_date < " & calendar1.SelectedDate.Date.AddDays(1)
Or using the BETWEEN operator: 或使用BETWEEN运算子:
SqlDataSource1.SelectCommand = "Select * from table1 where start_date between " & calendar1.SelectedDate.Date & " AND " & calendar1.SelectedDate.Date.AddDays(1)
Formatting only matters when you are displaying data. 格式化仅在显示数据时才重要。 Basically, dates are integers and datetimes are floats.
基本上,日期是整数,日期时间是浮点数。
Create a .net DateTime object from whatever is selected in your datepicker. 根据在日期选择器中选择的内容创建一个.net DateTime对象。 Then convert it to a query parameter which you use in your query.
然后将其转换为您在查询中使用的查询参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.