SqlCommand.Parameters.AddWithValue
方法注射安全吗?
它接受有效负载的Object
,那么它如何防止注入?
答案 0 :(得分:6)
实际上取决于你如何使用它们。
在这里看到差异。第一个是安全的,但不是第二个。
sqlCommand.CommandText = "select * from Books where Title = @title";
sqlCommand.Parameters.AddWithValue("title", txtTitle.Text);
string sql = "select * from Books where title = " + txtTitle.Text;
sqlCommand.CommandText = "exec(@sql)";
sqlCommand.Parameters.AddWithValue("sql", sql);