Home > Database > Mysql Tutorial > body text

Access日期字段参数化查询的怪问题!

WBOY
Release: 2016-06-07 15:43:37
Original
1025 people have browsed it

先看一下示例的SQL语句: select ID from (select *,(select top 1 订购 日期 from 订单 where ...) as 最近订购 from 客户) where 最近订购 between @p1 and @p2 对应的添加 参数 的代码(使用Enterprise Library 3.1): db.AddInParameter(cmd, p1, DbTyp

先看一下示例的SQL语句:

select ID from (select *,(select top 1 订购日期 from 订单 where ...) as 最近订购 from 客户)  where 最近订购 between @p1 and @p2

对应的添加参数的代码(使用Enterprise Library 3.1):

db.AddInParameter(cmd, p1, DbType.DateTime, dt1);

db.AddInParameter(cmd, p2, DbType.DateTime, dt2);

为了便于阅读,我简化了语句的写法,各位请别管语法问题,我这儿要描述的问题是:使用这个SQL命令来搜索记录,不会抛出异常,但搜索结果是不正确的!而如果把SQL中的参数p1、p2直接改为#2009-2-12 12:00:00#这种形式,结果就正确了!

换句话说,使用参数方式添加参数值搜索结果不正确;直接把时期值写到SQL语句中,就正确了。这是怎么回事呢?

补充说明:1、不使用子查询的话,参数查询是正确的;2、将DbType.DateTime改为DbType.Date,或者将dt1改为dt1.ToString()同样不能解决。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!