SqlCommand.Parameters.AddWithValue注入安全吗?

CJ7*_*CJ7 2 .net parameters sql-injection sqlcommand sql-parametrized-query

SqlCommand.Parameters.AddWithValue方法注射安全吗?

它接受Object有效载荷,所以它如何防止注入?

Tar*_*zam 6

这实际上取决于你如何使用它们.

看到这里的差异.第一个是安全的,但不是第二个.

sqlCommand.CommandText = "select * from Books where Title = @title";
sqlCommand.Parameters.AddWithValue("title", txtTitle.Text);
Run Code Online (Sandbox Code Playgroud)
string sql = "select * from Books where title = " + txtTitle.Text;
sqlCommand.CommandText = "exec(@sql)";
sqlCommand.Parameters.AddWithValue("sql", sql);
Run Code Online (Sandbox Code Playgroud)

asp.net上查看有关它的更多详细信息,以防止SQL注入攻击