执行带有日期的 SQLWhere 语句时出现问题

发布于 2025-01-08 21:37:20 字数 441 浏览 4 评论 0原文

当我对值进行硬编码并在 Access 2010 中执行 SQL 语句时,将执行该命令并返回所需的值。

选择 [活动 ID]、[人群规模]、[活动名称] 来自事件 WHERE ([Event Date]=#8/11/2012#) AND ([Event Name]="Peter")

但是,当我使用文本框代替硬编码值时

("SELECT [Event ID]、[人群规模]、[活动名称] FROM [活动] WHERE ([活动名称]='" + textBoxEventName.Text + "') AND ([活动日期]=#" + textBoxEventDate.Text + "#)");

当尝试从数据读取器读取时,它无法执行此操作,因为其中似乎没有任何值。

我已经在调试模式下检查了代码,文本框中的值是正确的,但仍然没有发生。

When I hardcode the values and execute the SQL statement within Access 2010, the command is executed and the values required are returned.

SELECT [Event ID], [Crowd Size], [Event Name]
FROM Event
WHERE ([Event Date]=#8/11/2012#) AND ([Event Name]="Peter")

However when I use text boxes in place of the hardcoded values

("SELECT [Event ID], [Crowd Size], [Event Name] FROM [Event] WHERE ([Event Name]='" + textBoxEventName.Text + "') AND ([Event Date]=#" + textBoxEventDate.Text + "#)");

When trying to read from the datareader, it is unable to do so as there doesn't seem to be any values in there.

I have gone through the code in debug mode and the values in the text boxes are correct, but still doesn't happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光暖心i 2025-01-15 21:37:20

Access 运行的 SQL 查询有点棘手...

确保您获取的日期格式正确,或者自行指定格式。

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx (标准日期和时间格式字符串)

还要确保有所有必要的引号,并且您没有使用任何“保留字”

http://support.microsoft.com/kb/286335(Access 中的保留字列表)

Access is a bit tricky with the SQL Queries that it runs...

Make sure that the date you are grabbing is in the correct format, or assign it a format your-self.

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx (Standard Date and Time Format Strings)

also make sure that there are all the quotation marks necessary and that you are not using any of the "reserved words"

http://support.microsoft.com/kb/286335 (List of reserved words in Access)

傲世九天 2025-01-15 21:37:20

经过几个小时的摆弄这个问题后,

解决方案是 Access SQL 喜欢以下格式的日期

#YYYY/MM/DD#

After many hours of fiddling around with this problem,

The solution is that Access SQL likes dates in the format

#YYYY/MM/DD#
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文