我在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)更多推荐
发布评论