C#GridView,Combobox日期范围(C# GridView, Combobox Date Range)

编程入门 行业动态 更新时间:2024-10-06 18:23:14
C#GridView,Combobox日期范围(C# GridView, Combobox Date Range)

我在C#WinForms中的日期范围查询有问题。 如果我选择03/26/2015作为开始日期,将2015年3月27日作为两个组合框的结束日期,则仅显示03/27/2015的所有数据,而不显示2015年3月26日至03日的数据/ 27/2015

这是代码:

DateTime date = DateTime.Now; dateTimePicker.Value = date; dateTimePicker.Format = DateTimePickerFormat.Custom; dateTimePicker.CustomFormat = "MM/dd/yyyy"; DateTime date2 = DateTime.Now; dateTimePicker2.Value = date2; dateTimePicker2.Format = DateTimePickerFormat.Custom; dateTimePicker2.CustomFormat = "MM/dd/yyyy"; SQL patients.entry_datetime >= @startDate and patients.entry_datetime <= @endDate cmd.Parameters.AddWithValue("@startDate", dateTimePicker.Value); cmd.Parameters.AddWithValue("@endDate", dateTimePicker2.Value);

I have problem on date range query in C# WinForms. If i select 03/26/2015 as start date and 03/27/2015 as end date on the two combo boxes, it shows all the data for 03/27/2015 only and not the data from 03/26/2015 to 03/27/2015.

Here is the code:

DateTime date = DateTime.Now; dateTimePicker.Value = date; dateTimePicker.Format = DateTimePickerFormat.Custom; dateTimePicker.CustomFormat = "MM/dd/yyyy"; DateTime date2 = DateTime.Now; dateTimePicker2.Value = date2; dateTimePicker2.Format = DateTimePickerFormat.Custom; dateTimePicker2.CustomFormat = "MM/dd/yyyy"; SQL patients.entry_datetime >= @startDate and patients.entry_datetime <= @endDate cmd.Parameters.AddWithValue("@startDate", dateTimePicker.Value); cmd.Parameters.AddWithValue("@endDate", dateTimePicker2.Value);

最满意答案

即使使用CustomFormat格式化的DateTime ,从dateTimePicker.Value传递的值也将采用DateTime格式而不是Date格式。 所以你需要在SQL查询中转换DateTime才能解决你的问题。 (假设您已将patients.entry_datetime存储为SQL Server中的DateTime 。

所以尝试像这样的SQL查询,

patients.entry_datetime >= CAST(@startDate AS DATE) AND patients.entry_datetime <= CAST(@endDate AS DATE)

Even though the DateTime formatted with CustomFormat the value passed from dateTimePicker.Value will be in DateTime format not Date format. So you need to convert DateTime in SQL query will solve your issue. (Assuming you have stored the patients.entry_datetime as DateTime in your SQL server.

So try the SQL query like this,

patients.entry_datetime >= CAST(@startDate AS DATE) AND patients.entry_datetime <= CAST(@endDate AS DATE)

更多推荐

本文发布于:2023-07-09 10:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085542.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   Combobox   GridView   Date   Range

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!