我如何使用之间 [英] how i can use between

查看:57
本文介绍了我如何使用之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"select * from voucher where trndate between '" & Format(Me.dtp_from.Value, "MM/dd/yyyy") & "' AND '" & Format(Me.dtp_to.Value, "MM/dd/yyyy") & "'"




我正在使用之间根据用户选择的这两个日期之间的时间来查找数据.但是我对此查询未获得任何结果.
请帮我.

dtp_from是我的第一个约会时间选择器
dtp_to是第二个.

用户将从dtp_from和dtp_to选择日期




I am using between to find data according between these two dates which slelct by the user.but i m not gaining any result against this query.
plz help me.

dtp_from is my first date time picker
dtp_to is the 2nd one.

user will select date from dtp_from and dtp_to

推荐答案

尝试以下操作:
Try this:
"select * from voucher where trndate between #" & Format(Me.dtp_from.Value, "MM/dd/yyyy") & "# AND #" & Format(Me.dtp_to.Value, "MM/dd/yyyy") & "#"



我用#



i have replaced '' with #


替换了".首先要注意的是BETWEEN包含第一个日期,而Exclusive包含第二个日期.因此,如果您的用户指定
First thing to note is that BETWEEN is Inclusive of the first date, and Exclusive of the second. So if your user specifies
BETWEEN 2011-02-01 AND 2011-02-02

仅返回2月1日的条目.

此外,请勿将日期转换为任何格式:如果您将其作为DateTime对象,则请使用参数化查询将其传递给原样.在这种情况下,您不会受到SQL注入攻击的威胁,但是始终值得避免连接字符串以防止意外或故意破坏数据库.

Only those entries for February 1st will be returned.

In addition, do not convert dates to any format: if you have them as DateTime objects, then pass that through as is using parametrized queries. In this case, you are not at risk from SQL injection attacks, but it is well worth always avoiding concatenated strings to prevent accidental or deliberate destruction of your database.

SELECT * FROM voucher WHERE trndate BETWEEN @DT1 AND @DT2


SelectCommand.Parameters.AddWithValue("@DT1", Me.dtp_from.Value)
SelectCommand.Parameters.AddWithValue("@DT2", Me.dtp_to.Value)

(这也使代码更易于阅读)

(It also makes the code easier to read)


尝试使用"yyyy-MM-dd"作为日期的格式字符串.

还要在数据库管理器应用程序上执行查询字符串,以查看是否有任何行.
Try using "yyyy-MM-dd" as a format string for your dates.

Also execute your query string on your database manager application to see if you have any rows.


这篇关于我如何使用之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆