Closed
Description
MySqlConnector version: 1.3.11
Environment: .NET Framework 4.7.2 under Windows 10
MySqlCommand.LastInsertedId
gives an incorrect result, when using multiple SQL-statements in the command.
This is the method that exposes the problem:
protected long NewEntryTransactional(string qry, params MySqlParameter[] parameters)
{
using MySqlConnection dbConnection = CreateDatabaseConnection();
using MySqlCommand cmd = CreateNewEntryCmd(dbConnection, qry, parameters); // Builds the cmd
dbConnection.Open();
try
{
MySqlTransaction tr = dbConnection.BeginTransaction();
try
{
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
long id = cmd.LastInsertedId;
tr.Commit();
return id;
}
catch (Exception)
{
tr.Rollback();
throw;
}
}
finally
{
dbConnection.Close();
}
}
When I call this method with qry
containing a SELECT statement followed by an INSERT statement, then cmd.LastInsertedId
returns -1 instead of the ID of the inserted row. I have also tried to moved long id = cmd.LastInsertedId;
behind tr.Commit();
But this still returns -1. (The SELECT statement defines a variable that is used in the INSERT statement.)
Running the code with Connector/NET returns the ID. (EDIT: My original Connector/NET code did not include the line cmd.Transaction = tr;
)