使用mysql编写的命令通过c#调用并从存储的mysql函数中获取返回值

时间:2011-03-15 14:20:35

标签: c# mysql return-value prepared-statement stored-functions

我相信我的第一篇文章不够清楚所以我改写了,这里是:

我在MySql中有一个存储函数。我想通过Prearped MySqlCommand调用这个存储的函数。我创建了一个mysql命令,可以调用存储的函数并准备它。 Aftwerwards我设置我的参数值并调用这个存储的函数。函数没有错误,但我只是无法获得返回值。我怎样才能获得回报价值?

这不是性能问题,我有一个实现,它在启动时准备所有语句。现在我想将这些功能添加到存储的过程和函数中。


这是我的存储功能:

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

为什么我无法获得回报价值?谢谢大家..

1 个答案:

答案 0 :(得分:-1)

你应该像选择

一样使用
select RenameCharacter(1,2,3,4);