2015-02-23 43 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對象。