简体   繁体   English

VB.Net中的SQL语句错误

[英]SQL statement error in VB.Net

This is the SQL statement which I taken from debug mode in VB.Net. 这是我从VB.Net的调试模式中获取的SQL语句。

SELECT  dt_Date as 'Date', 
        s_Time as 'Time', 
        s_For as 'For', 
        s_Categ as 'Category', 
        s_Count as 'Count', 
        s_Remarks as 'Remarks' 
FROM Entry 
WHERE (s_ENo = '22' and dt_date BETWEEN '06-05-16' And '27-05-16')

I am not sure what's wrong with the above statement since everything seems to be fine to my eyes. 我不确定上述说法有什么问题,因为我觉得一切都很好。

Description of the Error Message 错误消息说明

Additional information: Conversion failed when converting date and/or time from character string. 附加信息:从字符串转换日期和/或时间时转换失败。

Any suggestions how to solve this riddle? 有什么建议可以解决这个难题吗?

Instead if string literals, use strongly-typed parameters. 如果使用字符串文字,则应使用强类型参数。 In addition to avoiding data type conversion issues on the SQL Server side, parameters: 除了避免SQL Server端的数据类型转换问题外,参数:

  • Avoid the need to format date/time string literals in a particular way 避免以特定方式格式化日期/时间字符串文字
  • Improve performance by providing execution plan resue 通过提供执行计划来提高绩效
  • Prevent SQL injection (barring dynamic SQL on the server side) 防止SQL注入(在服务器端禁止动态SQL)

If you are using MS-SQL server than use following instructions. 如果您使用的是MS-SQL服务器,请按照以下说明进行操作。

The basic solution is that you have to provide date into either mm/DD/YYYY format or in YYYY-MM-DD date format into ms-sql query. 基本解决方案是您必须将日期提供为mm / DD / YYYY格式,或者将YYYY-MM-DD日期格式提供给ms-sql查询。

So, before passing date to query convert your date into either mm/DD/YYYY format or in YYYY-MM-DD format. 因此,在通过日期查询之前,将日期转换为mm / DD / YYYY格式或YYYY-MM-DD格式。

您必须将日期字符串格式设置为YYYYMMDD,或者如果您知道服务器日期格式,则与服务器格式相同

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

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