How to use wildcards in SQL query with parameters

后端 未结 3 367
半阙折子戏
半阙折子戏 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:29

    The %s should be part of the search string, not the query.

    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));
    

提交回复
热议问题