-
Notifications
You must be signed in to change notification settings - Fork 347
Urgent: Prepared MySqlCommands don't work #1105
-
Hi,
So I'm using .NET core 6.0 and MySqlConnector 2.1.1 (Visual Studio 2022) so the latest of each connecting to MySQL 8.0.
I can create and run commands successfully WITHOUT using .Prepare()
But as soon as I use .Prepare() the command object gets set to a null reference. No exception is thrown other than when trying to subsequently run a method on the command object (which is now null). So the .Prepare() is causing an issue that obviously just wipes the command object, and without reporting an error.
I've tried this in a C# managed project and a C++/CLI project using different variations of commands etc... But all do the same thing. MySQL's log reports that the PREPARE command is being sent to MySQL (successfully) but then the actual MySqlCommand gets set to null for some reason. (Note that the same code but using MySql's own MySql.Data package works find with Prepare but unfortunately it doesn't yet support net core 6.0 and obviously isn't as quick.)
Could anyone help please? Or provide some sample code using .Prepare() that they know to work please?
Thank you in advance.
Here's my code:
Obviously this is C++/CLI but the exact same issue is present when using pure managed C#.
MySqlCommand^ cmdTest = gcnew MySqlCommand();
cmdTest->Connection = cn;
cmdTest->CommandType = System::Data::CommandType::Text;
//I've tried using ? and @ptid as the parameter placeholder.
//The command ALWAYS works unless I call Prepare()
cmdTest->CommandText = "select count(0) from dpi_data_manager.master_parts mp "
+ "where mp.part_type_target_id = ?";
//This line successfully sends a PREPARE statement to MySQL as can be seen in the general log
//I've tried putting this line before and after adding the parameter below
cmdTest->Prepare();
MySqlParameter^ p = cmdTest->Parameters->Add("@ptid", MySqlDbType::Int32);
p->Value = 17;
//This line ALWAYS throws a null reference exception whatever variations I use - unless I don't call Prepare()
System::Int64 n64 = (System::Int64)cmdTest->ExecuteScalar();
Beta Was this translation helpful? Give feedback.
All reactions
Can discuss at #1106; no need to open duplicates.
Replies: 1 comment 1 reply
-
Can discuss at #1106; no need to open duplicates.
Beta Was this translation helpful? Give feedback.
All reactions
-
This is a bug in 2.1.0 that's fixed in 2.1.2.
Beta Was this translation helpful? Give feedback.