How to use wildcards in SQL query with parameters

后端 未结 3 372
半阙折子戏
半阙折子戏 2020-12-06 16:47

Say I have a basic query, something like this:

 SELECT holiday_name
 FROM holiday
 WHERE holiday_name LIKE %Hallow%

This executes fine in m

3条回答
  •  孤城傲影
    2020-12-06 17:41

    whatever you do don't do this:

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

    as that will open you up to sql injection, instead do this:

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

    you may like to know about Command.Parameters.AddWithValue, e.g:

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

提交回复
热议问题