我有一个看起来像这样的存储过程
BEGIN
insert into `TheSomaProject`.`Projects`
( `ProjectNr`, `MachineName`, `MachineNameEng`, `Type_Id`,
`SerialNr`, `Description`, `Created`, `CompanyId`, `MachineType` )
values
( param_ProjectNr, param_MachineName, param_MachineNameEng, param_TypeId,
param_Serial, param_Description, NOW(), param_CompId, param_MachineType);
SELECT last_insert_id();
END
但是返回值给了我 ID,它看起来像这个 0x00000000。C# 代码如下所示
MySqlDataReader Reader;
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = "CALL stp_InsertProject(@param_ProjectNr, @param_MachineName, @param_MachineNameEng, @param_TypeId, @param_Serial, @param_Description, @param_CompId, @param_MachineType);";
cmd.Parameters.AddWithValue("@param_ProjectNr", projectNr);
cmd.Parameters.AddWithValue("@param_MachineName", MachineName);
cmd.Parameters.AddWithValue("@param_MachineNameEng", MachineNameEng);
cmd.Parameters.AddWithValue("@param_TypeId", TypeId);
cmd.Parameters.AddWithValue("@param_Serial", SerialNr);
cmd.Parameters.AddWithValue("@param_Description", Description);
cmd.Parameters.AddWithValue("@param_CompId", CompanyId);
cmd.Parameters.AddWithValue("@param_MachineType", MachineType);
conn.Open();
Reader = cmd.ExecuteReader();
while (Reader.Read())
{
Id = Convert.ToInt32(Reader["last_insert_id()"]);
}
conn.Close();
但是,如果我尝试在 phpMyAdmin 中使用相同的 stp_InsertProject,我会得到正确的 id。