简体   繁体   English

如何在两个日期时间选择器的日期之间选择查询?

[英]How to do select query between dates of two datetime picker?

I have to datetimepickers FromDate and ToDate, and want to do select data between to dates, I have written the query 我必须datetimepickers FromDate和ToDate,并且想要在日期之间选择数据,我已经编写了查询

select  * from tbl where pDate>='" + dtpFrom.value + "'and pDate<='" + dtpTo.value + "'");

This query giving error 该查询给出错误

Data type mismatch in criteria expression 条件表达式中的数据类型不匹配

But datatype is Date/Time in ms access table. 但是数据类型是ms访问表中的日期/时间。

Looks like you try to put single quotes for your DateTime values. 看起来您尝试为DateTime值加上单引号。 # for dates and ' for strings but they required for literal SQL queries. #代表日期, '代表字符串, 但是它们是文字 SQL查询所必需的。

If you use parameterize queries, you will not need them. 如果使用参数化查询,则将不需要它们。

using(var con = new OleDbConnection(conString))
using(var cmd = con.CreateCommand())
{
   cmd.CommandText = "select * from tbl where pDate >= ? and pDate <= ?"
   cmd.Parameters.AddWithValue("?", dtpFrom.Value);
   cmd.Parameters.AddWithValue("?", dtpTo.Value);
   ...
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM