StrSqlLines = "Select * From dbo.SqlDtaToCreate WHERE DtaLineAccountToDebit = '" + Straccref + "' and DtaLineCode IN('" + joined + "')";执行此查询时出现错误
Conversion failed when converting the varchar value '116743,116744,116745' to data type int.但是,该查询在SQL中运行良好
select * from SqlDtaToCreate where DtaLineAccountToDebit='123.567U' and DtaLineCode IN('116745','116746','116747')发布于 2013-02-28 19:16:58
首先:
这可能是一个糟糕的注入案例!使用准备好的语句(或者至少正确转义)!
第二:
当前的问题是IN子句的封闭'是否正确:
StrSqlLines = "Select * " +
"From dbo.SqlDtaToCreate "
" WHERE DtaLineAccountToDebit = '" + Straccref + "' " +
" and DtaLineCode IN(" + joined + ")"; //notice missing ' characters由'字符包围的任何内容都被视为单个字符串。SQL server尝试将此单个字符串解析为数字,但由于无法解析,因此报告了此错误。
第三:
在使用数字数据时,永远不会(我有没有提到过 never 呢?)使用文本数据来与之进行比较--这可能会扼杀性能。(在这种规模下,这当然不重要,但记住这一点可以节省大量不必要的性能分析和调试……)
因此,当这个查询实际工作时:
select * from SqlDtaToCreate where DtaLineAccountToDebit='123.567U' and DtaLineCode IN('116745','116746','116747')它对所提供的数据进行隐式转换,因此正确的方法是:
select * from SqlDtaToCreate where DtaLineAccountToDebit='123.567U' and DtaLineCode IN(116745,116746,116747)发布于 2013-02-28 19:16:41
请注意,joined变量中缺少'。
'116743,116744,116745‘
'116745','116746','116747‘
理想情况下,由于您比较的是整数,所以您需要的是
116743,116744,116745
并阅读有关sql injection的内容。
发布于 2013-02-28 19:17:21
这是对连接的术语的转换,似乎数据库服务器可以转换包含在引号中的数字,但不能超过逗号分隔的数字,在您的情况下:
'116743,116744,116745'至:
'116745','116746','116747'尝试重新格式化joined以用引号分隔数字,或者从sql语句中删除引号,以便得到IN(" + joined + ")"而不是:IN('" + joined + "')"
https://stackoverflow.com/questions/15134056
复制相似问题