我相信我的第一篇文章不够清楚,所以我重写了,这里是:
我在 MySql 中有一个存储函数。我想通过 Prearped MySqlCommand 调用这个存储的函数。我创建了一个可以调用存储函数并准备它的mysql命令。之后我设置我的参数值并调用这个存储的函数。函数正常工作,但我无法获得返回值。我怎样才能有我的返回值?
这不是性能问题,我有一个在启动时准备所有语句的实现。现在我想将这些功能添加到存储过程和函数中。
这是我存储的函数:
FUNCTION `RenameCharacter`(pUserKey varchar(50), pPlayerId int,
pCharacterId int, pCharacterName varchar(50))
RETURNS int(11)
为了调用mysql存储函数,首先我创建一个mysql命令:
commandString = new MBCommandString();
commandString.commandName = "RenameCharacter";
commandString.commandString = "RenameCharacter";
/*@"Update characters set name = ?3
where characterId = ?2
and playerId = (Select playerId from players where lastKey = ?1)";*/
commandString.parameters = new MySqlParameter[5];
commandString.parameters[1] = new MySqlParameter("pUserKey", MySqlDbType.VarChar);
commandString.parameters[2] = new MySqlParameter("pPlayerId", MySqlDbType.Int32);
commandString.parameters[3] = new MySqlParameter("pCharacterId", MySqlDbType.Int32);
commandString.parameters[4] = new MySqlParameter("pCharacterName", MySqlDbType.VarChar);
commandString.parameters[0] = new MySqlParameter("@returnValue", MySqlDbType.Int32);
commandString.parameters[0].Direction = ParameterDirection.ReturnValue;
commandString.type = MBCommandType.MBCT_Function;
commandList.Add(commandString);
然后我准备它:
MBCommand cmd = new MBCommand();
cmd.command = new MySqlCommand(cs.commandString, connection);
cmd.command.Parameters.AddRange(cs.parameters);
if (cs.type == MBCommandType.MBCT_Function)
{
cmd.command.CommandType = CommandType.StoredProcedure;
}
cmd.command.Prepare();
cmd.type = cs.type;
commands.Add(cs.commandName, cmd);
我使用类 c# 调用它,函数有效,但我无法获得返回值:
string key = reader.ReadString();
int playerId = reader.ReadInt32();
int characterId = reader.ReadInt32();
string characterNewName = reader.ReadString();
MBExecuteSingle execute = new MBExecuteSingle();
execute.commandName = "RenameCharacter";
execute.values = new object[5];
execute.values[1] = key;
execute.values[2] = playerId;
execute.values[3] = characterId;
execute.values[4] = characterNewName;
execute.values[0] = new Int32();
execute.values[0] = 4; //dummy initial value
为什么我不能有返回值缺少什么?谢谢你们..