[英]MySql output parameters in c#
I'm running the following code, that is meant to execute SET @Param = 3;
我正在运行以下代码,旨在执行
SET @Param = 3;
line in MySql, and get Param value 3. line in MySql,得到Param值3。
MySqlCommand setCommand = new MySqlCommand("SET @Param = 3;", connection);
MySqlParameter parameter = new MySqlParameter("Param", MySqlDbType.Int32);
parameter.Direction = System.Data.ParameterDirection.Output;
setCommand.Parameters.Add(parameter);
setCommand.ExecuteNonQuery();
Console.WriteLine("Parameter value: "+parameter.Value);
I'm quite new to MySql, but as far as I understood, the query SET @Param = 3;
我对 MySql 很陌生,但据我了解,查询
SET @Param = 3;
sets the @Param variable value to 3, and apart from my c# code it looks like it's working (in separate workbench).将 @Param 变量值设置为 3,除了我的 c# 代码外,它看起来正在工作(在单独的工作台中)。 But when running my code it throws the following error:
但是当运行我的代码时,它会抛出以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL = 3' at line 1
I believe that when trying to run this query SET @Param = 3;
我相信在尝试运行此查询时
SET @Param = 3;
it tries to fill @Param parameter that is null, instead of getting it's value.它尝试填充 null 的 @Param 参数,而不是获取它的值。 I've declared parameters Direction as Output and added it to the setCommand command Parameters, but it seems like I'm doing something wrong.
我已将参数 Direction 声明为 Output 并将其添加到 setCommand 命令参数中,但似乎我做错了什么。 I don't know where am I wrong, is it Direction issue, the @/?/none parameter declaration issue, or maybe it is MySqlDbType issue (as I'm not sure that MySqlDbType.Int32 corresponds to Int32 in C#)?
我不知道我哪里错了,是方向问题,@/?/none 参数声明问题,还是 MySqlDbType 问题(因为我不确定 MySqlDbType.Int32 是否对应于 C# 中的 Int32)? Please advise.
请指教。
It should be working in MS SQL, I've used it's tutorial and adapted it for MySql. After some research I've found a similar reported issue.它应该在 MS SQL 中工作,我已经使用它的教程并将其改编为 MySql。经过一些研究,我发现了一个类似的报告问题。 In MySql, it seems that Direction property of Parameter can't be Output if CommandType is Text -
@parameter
s wouldn't be output as intended.在 MySql 中,如果 CommandType 是 Text,Parameter 的 Direction 属性似乎不能是 Output -
@parameter
s 不会像预期的那样是 output。 You can set this property though, it just wouldn't work.您可以设置此属性,但它不起作用。 The resolution might be executing
SELECT @parameter;
该决议可能正在执行
SELECT @parameter;
after executing the main SET/UPDATE/etc.在执行主要的 SET/UPDATE/etc 之后。 parametrized queries to get values, or creating Stored Procedures.
参数化查询以获取值,或创建存储过程。
Reference: https://github.com/mysql.net/MySqlConnector/issues/231参考: https://github.com/mysql.net/MySqlConnector/issues/231
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.