MySqlCommand parameters do not work

In the following code, which is used to get a list of products on a specific line, the command returns results only when I hard-code (concatenate) productLinein SQL. Parameter substitution never occurs.

            + "lineName = '@productLine' "                       
            + "and isVisible = 1 ";
        MySqlDataAdapter adap = new MySqlDataAdapter(sql, msc);
        adap.SelectCommand.Parameters.Add("@productLine", productLine);
+3
source share
4 answers
        + "lineName = ?productLine "                       
        + "and isVisible = 1 ";
    MySqlDataAdapter adap = new MySqlDataAdapter(sql, msc);
    adap.SelectCommand.Parameters.Add("?productLine", productLine);
  • Remove the apostrophes (').
  • Change @ to ?, which is the prefix of the parameters in MySql queries.
+7
source

Delete apostrophes (spelling?). The parameter is around the parameter. They should not be needed.

+2
source

+ "lineName = '@productLine' " 

+ "lineName = @productLine " 
0

Is it right that you have

  • "lineName = '@productLine'"

to try

  • "lineName = @productLine" instead, since @productLine will already be declared as a string type, quotation marks will be added secretly. However, you are actually passing the @productLine string, not the value of the variable.
0
source

Source: https://habr.com/ru/post/1698923/


All Articles