如何在SQL查询中使用通配符和参数。

18

假设我有一个基本查询,就像这样:

 SELECT holiday_name
 FROM holiday
 WHERE holiday_name LIKE %Hallow%

这个在我的SQL查询窗口中可以正常执行,并返回"Halloween"。但是当我尝试在代码中使用带有通配符'%'的参数时,就会出现问题。

SqlConnection Connection = null;
SqlCommand Command = null;

string ConnectionString = ConfigurationManager.ConnectionStrings["SQLdb"].ConnectionString;
string CommandText = "SELECT holiday_name "
                   + "FROM holiday "
                   + "WHERE holiday_name LIKE %@name%";
Connection = new SqlConnection(ConnectionString);

try
{
      Connection.Open();
      Command = new SqlCommand(CommandText, Connection);
      Command.Parameters.Add(new SqlParameter("name", HolidayTextBox.Text));
      var results = Command.ExecuteScalar();
}

catch (Exception ex)
{   
     //error stuff here       
}

finally
{
    Command.Dispose();
    Connection.Close();
}

出现了语法错误。我尝试将“%”移动到参数中,如下所示:

Command.Parameters.Add(new SqlParameter("%name%", HolidayTextBox.Text));

但是我收到了一个错误,说我没有声明标量变量'@name'。那么,如何正确地格式化通配符字符以与查询参数一起使用?任何帮助都将不胜感激!

3个回答

28

首先,你的 SqlParameter 名称是 @name 而不是 name

其次,我建议移动通配符的位置。

因此,它应该看起来像这样:

string CommandText = "SELECT holiday_name "
               + "FROM holiday "
               + "WHERE holiday_name LIKE @name;"
Connection = new SqlConnection(ConnectionString);

try
{
  var escapedForLike = HolidatyTextBox.Text; // see note below how to construct 
  string searchTerm = string.Format("%{0}%", escapedForLike);
  Connection.Open();
  Command = new SqlCommand(CommandText, Connection);
  Command.Parameters.Add(new SqlParameter("@name", searchTerm));
  var results = Command.ExecuteScalar();
}

请注意,LIKE语句在传递参数时需要特别小心,您需要转义一些字符。使用 SQL 参数转义 SQL LIKE 语句中的特殊字符。


12

无论做什么,都不要做这件事

string CommandText = "SELECT holiday_name "
                   + "FROM holiday "
                   + "WHERE holiday_name LIKE '%'" + HolidayTextBox.Text + "'%'";

因为这会使你容易受到SQL注入攻击,所以请改成这样:

Command.Parameters.Add(new SqlParameter("@name", "%" + HolidayTextBox.Text + "%"));

你可能想了解 Command.Parameters.AddWithValue,例如:

Command.Parameters.AddWithValue("@name", "%" + HolidayTextBox.Text + "%");

2
我不建议使用SQL注入。 - Tim S.
8
小鲍比·泰布尔喜欢 SQL 注入! - Ryan Anderson
1
它确实非常安全。 - zerkms
2
@zerkms 允许用户提供输入,因此像 DELETE FROM users WHERE userName LIKE '%' + @var + '%' 这样的语句仍然存在很大危险... (实际上,在使用用户输入时,需要过于信任每个人,尤其是在更新语句中使用 LIKE 可能会遭到应有的报应 :)) - Alexei Levenkov
1
你可能想了解关于AddWithValue的一些事情:AddWithValue是邪恶的AddWithValue是邪恶的!,以及我们能否停止使用AddWithValue()了? - Andrew Morton
显示剩余5条评论

2
"%s" 应该是搜索字符串的一部分,而不是查询。"
string CommandText = "SELECT holiday_name "
                + "FROM holiday "
                + "WHERE holiday_name LIKE @name";
Connection = new SqlConnection(ConnectionString);

try
{
    Connection.Open();
    Command = new SqlCommand(CommandText, Connection);
    string name = "%" + HolidayTextBox.Text + "%";
    Command.Parameters.Add(new SqlParameter("@name", name));

它可以保护一些情况,但不足以发挥作用。如果我在“HolidayTextBox.Text”中输入“__ck”会怎样?(请参见https://dev59.com/WIPba4cB1Zd3GeqPoSnm) - Alexei Levenkov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接