我正在使用以下代码:
string str = "ID = 15";
using (SqlCommand cmd = new SqlCommand("SELECT * " +
"FROM Cutomer" +
" Where (Status='Umbruch') and '"+str+"' " , con))
我收到此错误:
near 'ID = 15' is a non boolean expression specified in a context where a condition is expected.
我知道我可以像这样使用上面的代码:
int id = 15;
using (SqlCommand cmd = new SqlCommand("SELECT * " +
"FROM Cutomer" +
" Where (Status='Umbruch') and (ID='"+str+"')" , con))
这样工作正常,但我想按上面第一个代码中所述使用它。由于ID的数量不是固定的,因此可以为2,3或更大。为了简单起见,我将代码放在这里。
您在这里将其设为文字:
using (SqlCommand cmd = new SqlCommand("SELECT * " +
"FROM Cutomer" +
" Where (Status='Umbruch') and '"+str+"' " , con))
它应该是:
using (SqlCommand cmd = new SqlCommand("SELECT * " +
"FROM Cutomer" +
" Where (Status='Umbruch') and "+str, con))
请注意'
删除的细微差别。
此外,请阅读我的博客文章,了解为什么您愿意接受SQL注入以及为什么需要利用参数化SQL而不是原始SQL。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句