0

I am having the weirdest issue with MySQL and my C# web application...

Whenever I call this method without the PrepareAsync(), then it returns my data perfectly fine.

However when I do use the PrepareAsync() method, it gives me a MySQL Exception:

MySql.Data.MySqlClient.MySqlException (0x80004005): Expected end of data packet

public async Task Test()
{
    using MySqlConnection conn = new MySqlConnection(connection);
    conn.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT * FROM characters WHERE deleted=0 ORDER BY @columnName DESC", conn);
    await cmd.PrepareAsync(); // <--------------- HERE
    cmd.Parameters.AddWithValue("@columnName", "alchemyXP");
    using var reader = await cmd.ExecuteReaderAsync();

    while (await reader.ReadAsync())
    {
        var v = reader["alchemyXP"];
        var vv = reader["name"];
    }
}

My MySQL Server version is: 8.0.32

My MySQL Server + all tables are charset: utf8mb4

My MySQL.Data NuGet package is: 8.3.0

My MySQL connection string in the application is:

server=10.0.0.12;uid=test123;pwd=pass123;database=testdb123;CharSet=utf8mb4;

8
  • Check this thread github.com/mysql-net/MySqlConnector/issues/397 Connector/NET has a IgnorePrepare connection string setting that defaults to true; unless the user explicitly sets that to false then its Prepare method also does nothing. Commented Apr 22, 2024 at 5:17
  • 1
    Try to add parameters first and then call prepare. also, AddWithValue is not very good option. Do cmd.Parameters.Add( with concrete data type Commented Apr 22, 2024 at 5:24
  • lets start with MySql.Data.MySqlClient can have many bugs some of which I have reported bugs.mysql.com/bug.php?id=92912 Commented Apr 22, 2024 at 5:27
  • According to 12.3.2 Server Character Set and Collation: MySQL Server has a server character set and a server collation. By default, these are utf8mb4 and utf8mb4_0900_ai_ci. The following may also be of interest: 12.3.8 Character Set Introducers and 12.4 Connection Character Sets and Collations Commented Apr 22, 2024 at 6:54
  • 2
    You can't parameterize column names in a SQL statement. ORDER BY @columnName will become ORDER BY 'alchemyXP' when the parameter is bound, which is just ordering by a constant, ignoring the data in the table. You will have to build this "dynamic" statement with string concatenation, whether or not you prepare the MySqlCommand. Commented Apr 22, 2024 at 22:52

1 Answer 1

2

Bradley Grainger's comment was the correct solution for my issue:

You can't parameterize column names in a SQL statement. ORDER BY @columnName will become ORDER BY 'alchemyXP' when the parameter is bound, which is just ordering by a constant, ignoring the data in the table. You will have to build this "dynamic" statement with string concatenation, whether or not you prepare the MySqlCommand.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.