Closed
Description
MySqlConnector version: 2.1.0
MariaDB: 10.6.5
.NET versions: 4.6.2, 4.8 and 5.0
The MySqlConnector does not calculate rows-affected during batch sql correctly. Depending on the order of the sql queries in the batch the rows-affected value is different.
-- DB create Table and StoredProcedure for this demo
-- Create Table
CREATE TABLE `test`.`Demo` (
`Id` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR (50) NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE = INNODB CHARSET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = DYNAMIC;
-- Create StoredProcedure
DELIMITER $$
CREATE PROCEDURE `test`.`SP_Demo`(in pId INT, IN pName VARCHAR(50))
sp_master_label:BEGIN
DECLARE dResultCode INT DEFAULT 0; -- 1 = Execution related error
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
-- Error occured
SET dResultCode = 1;
SELECT dResultCode AS ResultCode;
END;
UPDATE `Demo` SET `Name` = pName WHERE (`Id` = pId);
SET dResultCode = 0;
SELECT dResultCode AS ResultCode;
END$$
DELIMITER ;
-- Pre-Add a row for the demo
INSERT INTO `test`.`Demo` (`Name`) VALUES ('Demo-Name');
// ------------------- C# Part
string connectionString = "Server=localhost;Port=3306;Database=test;User ID=<UserId>;Password=<Password>";
string sqlSp = "CALL `SP_Demo`(1, 'Demo-Name-Updated');";
string sqlInsert = "INSERT INTO `Demo` (`Name`) VALUES ('Demo-Name-Updated-Batch');";
int rowsAffected = 0;
// A batch with SP + Insert returns -1
rowsAffected = ExecuteSql(connectionString, sqlSp + sqlInsert); // rowsAffected = -1
// Reversing batch order with Insert + SP returns 1
rowsAffected = ExecuteSql(connectionString, sqlInsert + sqlSp); // rowsAffected = 1
// A batch with SP + SP + Insert returns 1
rowsAffected = ExecuteSql(connectionString, sqlSp + sqlSp + sqlInsert); // rowsAffected = 1
private int ExecuteSql(string connectionString, string sql)
{
int rowsAffected = 0;
using (MySqlConnector.MySqlConnection cn = new MySqlConnector.MySqlConnection(connectionString))
{
cn.Open();
using (MySqlConnector.MySqlCommand cmd = new MySqlConnector.MySqlCommand(sql, cn))
{
cmd.CommandType = CommandType.Text;
rowsAffected = cmd.ExecuteNonQuery();
}
cn.Close();
}
return rowsAffected;
}
I expect that a batch sql should return the same value for rows-affected no matter the order in which the sqls are executed if they does not affect each other.
Metadata
Metadata
Assignees
Labels
No labels