我有一个格式化查询的严重问题,它可能会导致SQL注入,我看到一些类似的qstns,但不知道我怎么能在C#中使用它,因为我是新来的它。我使用C#,ODBC命令动态SQL查询形成,使用string.format
我有3个串像 qry ="select description from TableA" , qryOrder = " order by description" , qryAppend = " where ID = '{0}' order by description\", _selectedPlantID"
提供_selectedId
是另一个变量,现在我想用这些变量在不同的场景,形成差异查询,例如,qry + qry order
,或qry + qryAppend
。
由于_selectedPlantId
还需要,我用的String.format为:
_cmd.CommandText = "string.Format(\"" + qry + qryAppend + ")";
但它不工作。任何解决方案 错误是SQL引用的语法错误
在此先感谢!
使用[参数化查询](http://www.dotnetperls.com/sqlparameter)。 – DGibbs
仔细查看_cmd.CommandText的值。你可能实际上看到了问题。 –