这个存储过程是否可以安全地从SQL注入?

Jus*_*son 4 sql-server stored-procedures sql-injection

存储过程如下:

CREATE PROCEDURE Foo
    @bar varchar(100)
AS

SELECT * FROM tablename
WHERE columnname LIKE '%' + @bar + '%'
Run Code Online (Sandbox Code Playgroud)

我已经尝试将各种字符串传递给这个存储过程,但对我来说,看起来这样可以安全地从SQL注入,因为包含通配符之间的所有内容都会导致单个字符串.

Abe*_*ler 7

如果您使用的是C#,则代码如下所示:

SqlCommand command = new SqlCommand("Foo", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@bar", myTextBox.Text);
Run Code Online (Sandbox Code Playgroud)

好的!

如果它看起来像这样:

SqlCommand command = new SqlCommand("EXEC Foo '" + myTextBox.Text + "'", connection);
Run Code Online (Sandbox Code Playgroud)

那就不要!