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;
AddWithValueis not very good option. Docmd.Parameters.Add(with concrete data typeORDER BY @columnNamewill becomeORDER 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.