2015-02-23 94 views
2

我在C#中第一次在一段时间内工作,我有这样的一行代码。Secure string.Format对象C#

SQL = string.Format("PageName = '{0}'", bp.CommandArgument); 

我需要知道如何从任何SQL注入中保护对象“bp.CommandArgument”。谢谢。

+5

请勿首先使用字符串格式化/连接创建SQL查询,请使用[Parameters](https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlparameter%28v=vs 0.110%29.aspx)。 – Habib 2015-02-23 18:20:53

回答

6

为什么不使用sql参数?

string commandTxt = "SELECT ... FROM ... WHERE [email protected]"; 
var command = new SqlCommand(commandTxt, connection); 
command.Parameters.Add("@PageName", bp.CommandArgument); 

我认为connection是已宣布SqlConnection对象。