使用 C# 的 mysql 准备命令调用并从存储的 mysql 函数获取返回值

发布于 2024-10-21 22:44:50 字数 3021 浏览 2 评论 0原文

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

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

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


这是我的存储函数:

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

为了调用 mysql 存储函数,首先我创建一个 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);

然后准备它:

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

缺少什么为什么我不能有返回值吗?谢谢大家..

I believe my first post was not clear enough so I rewrite, here it 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-10-28 22:44:50

你应该像选择一样使用

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

You should use like a Select

select RenameCharacter(1,2,3,4);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文