MySqlCommand c# - How to ignore custom row parameter whilst replacing other params in command text

32 Views Asked by At

So I'm trying to use the following syntax to produce a row number for each result, where:

  • @row is created as the query runs and
  • @search is a parameter I've added myself:
var cmd = new MySqlCommand();
cmd.CommandText = @"SET @row = 0;
                    SELECT
                        (@row:= @row + 1) AS row,
                            *
                    FROM
                        clients
                    WHERE
                        name CONCAT('%', @search, '%')";

cmd.Parameters.AddWithValue("@search", "whatever");

try
{
    cmd.Connection.Open();
    using (var reader = cmd.ExecuteReader())
    {
        // blah blah
    }
}
catch (Exception ex)
{
    cmd.Connection.Close();

}  

This query works fine if I run the MySQL straight in the database, but I get an error from the c#:

Parameter @row must be defined

..if I use a MySqlCommand object.

Is there a way around this problem?

Thanks

0

There are 0 best solutions below