【发布时间】:2012-02-29 23:56:35
【问题描述】:
我正在尝试在 c# 2010 中调用一个简单的存储过程。 只有一个 IN 参数没问题,但现在使用 OUT 参数就不行了。
在 phpmyadmin 中:
drop procedure if exists insert_artist;
delimiter $$
create procedure insert_student(IN name VARCHAR(100), OUT id INT)
begin
insert into student(name) values(name);
set id = last_insert_id();
end$$
delimiter ;
然后使用
call insert_student("toto",@id);
select @id;
一切正常。
现在,在 c# 中:
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
using (MySqlCommand command = connection.CreateCommand())
{
command.CommandText = "insert_student";
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("@name", "xxxx");
command.Parameters.AddWithValue("@id",MySqlDbType.Int32);
command.ExecuteNonQuery();
Console.WriteLine("**** " + command.Parameters["@id"].Value);
}
}
执行ExecuteNonQuery() 时给我一个例外:
例程 insert_student 的 OUT 或 INOUT 参数 2 不是 BEFORE 触发器中的变量或 NEW 伪变量
在存储过程中没有 out 参数的同样的事情工作正常。 我的错在哪里?
【问题讨论】:
-
我不记得怎么做了,但是您需要在第二个参数上将
Direction或ParameterDirection属性(或类似的) 设置为Out. -
好的,它正在添加:command.Parameters["@id"].Direction = System.Data.ParameterDirection.Output;感谢 atornblad。
-
对于那些感兴趣的人,我展示了一个 MySQL / c# Visual Studio 2015 工作示例HERE。这种情况是 IN 和
OUT参数之一。焦点自然是OUT。