执行带有日期的 SQLWhere 语句时出现问题
当我对值进行硬编码并在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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)
试试这个:请参阅此页面: http://www.tizag.com/sqlTutorial/sqlselect.php< /a>
Try this: see this page: http://www.tizag.com/sqlTutorial/sqlselect.php
经过几个小时的摆弄这个问题后,
解决方案是 Access SQL 喜欢以下格式的日期
After many hours of fiddling around with this problem,
The solution is that Access SQL likes dates in the format