开发者

call and get return value from stored mysql function using a mysql prepared command by c#

I believe my first post was not clear enough so I rewrite, here i开发者_JS百科t is:

I have a stored function in MySql. I want to call this stored function via Prearped MySqlCommand. I create a mysql command which can call stored function and prepare it. Aftwerwards I set my parameter values and call this stored function. Function works without error, but I just cannt get return value. How can I have my return value?

This is not a performance issue, I have an implemantation which prepares all statements at startup. Now I wanna add these functiality to stored procedured and functions.


Here is my stored function:

FUNCTION `RenameCharacter`(pUserKey varchar(50), pPlayerId int, 
pCharacterId int, pCharacterName varchar(50)) 
RETURNS int(11)

In order to call a mysql stored function, first I create a mysqlcommand:

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);

Then I prepare it:

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);

And I call it using a class c#, function works but I cannot get the return value:

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

What's missing why I cannot have return value? Thank you all..


You should use like a Select

select RenameCharacter(1,2,3,4);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜