简体   繁体   English

按日期搜索Gridview,但日历控件选择器(即DateTime)同时包含日期和时间,例如:2013-01-01 00:00:00.000

[英]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 问题

  • Since the Calendar control's selected date is DateTime, unfortunately the selected date in the Calendar seems to be is in the format : dd-MM-yyy 由于Calendar控件的选定日期为datetime,可惜在选定日期Calendar似乎是格式为dd-MM-yyy
    example 2013-02-15 范例 2013-02-15
  • while the 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
    example 2013-02-15 09:02:03.253 范例 2013-02-15 09:02:03.253

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),但是可以这样做吗?


Edit: 编辑:

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.SelectedDateDateTime 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.

相关问题 日期时间“0001-01-01T00:00:00”的问题 - Problem with the DateTime "0001-01-01T00:00:00" ASP.Net API DateTime ISO8601 日期值在生产中被解析为默认日期 (01/01/0001 00:00:00) &amp; Azure - ASP.Net API DateTime ISO8601 date value being parsed as default date (01/01/0001 00:00:00) in production & Azure C#日期格式{0:t} - {0:t + 1} 00:00 - 01:00 - C# Date format {0:t} - {0:t+1} 00:00 - 01:00 C#DateTime默认值Issue 01/01/0001 00:00:00 - C# DateTime Default Value Issue 01/01/0001 00:00:00 HL7.FHIR.R4 (C#):无法将文字“2020-12-01T23:00:00.000Z”解析为日期。 (在 Bundle.entry[0].resource[0].birthDate[0]) - HL7.FHIR.R4 (C#): Literal '2020-12-01T23:00:00.000Z' cannot be parsed as a date. (at Bundle.entry[0].resource[0].birthDate[0]) 解析时间跨度 1:00 和 01:00 - Parse TimeSpan 1:00 and 01:00 提交的日期时间转换为“0001-01-01T00:00:00” - Submitted datetime is converted to "0001-01-01T00:00:00" sql datetime列-插入时间为00:00:00.000 - sql datetime column - insert time as 00:00:00.000 将可为空的 DateTime 字段更新为 null 会导致默认 DateTime 值 (0001-01-01 00:00:00.0000000) - Updating a nullable DateTime field as null results in default DateTime value (0001-01-01 00:00:00.0000000) 2001-01-01 00:00:00.000插入数据库而不是2000-12-31 23:59:59 - 2001-01-01 00:00:00.000 inserted into database instead 2000-12-31 23:59:59
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM