【问题标题】:Stored Procedures with Mysql connector带有 Mysql 连接器的存储过程
【发布时间】: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 参数的同样的事情工作正常。 我的错在哪里?

【问题讨论】:

  • 我不记得怎么做了,但是您需要在第二个参数上将DirectionParameterDirection 属性(或类似的) 设置为Out .
  • 好的,它正在添加:command.Parameters["@id"].Direction = System.Data.ParameterDirection.Output;感谢 atornblad。
  • 对于那些感兴趣的人,我展示了一个 MySQL / c# Visual Studio 2015 工作示例HERE。这种情况是 IN 和 OUT 参数之一。焦点自然是OUT

标签: c# mysql


【解决方案1】:

一个更完整的例子:

if (this.OpenConnection() == true)
{
     MySqlCommand cmd = new MySqlCommand(nameOfStoredRoutine, connection);    
     cmd.CommandType = CommandType.StoredProcedure;    
     //input parameters
     for (int i = 0; i < (parameterValue.Length / 2); i++)
     {
         cmd.Parameters.AddWithValue(parameterValue[i, 0], parameterValue[i, 1]);
         cmd.Parameters[parameterValue[i, 0]].Direction = ParameterDirection.Input;
         parameterList = parameterList + parameterValue[i,0] + " " + parameterValue[i,1] + " ";
      }

      //single output parameter
      cmd.Parameters.AddWithValue("@output", MySqlDbType.Int32);
      cmd.Parameters["@output"].Direction = ParameterDirection.Output;

      cmd.ExecuteNonQuery(); //Execute command
      this.CloseConnection(); //close connection

      return Convert.ToInt32(cmd.Parameters["@output"].Value.ToString());

【讨论】:

    【解决方案2】:

    我下面的代码有效

    请检查是否适合您。

    InsertQuery = New MySqlCommand("xxxxxx")
    InsertQuery.Connection = Connection
    InsertQuery.CommandType = Data.CommandType.StoredProcedure
    
    InsertQuery.Parameters.AddWithValue("IN_xxx", str_xxxx)
    
    InsertQuery.Parameters.Add("OUT_LastID", MySqlDbType.Int32).Direction = ParameterDirection.Output
    IQ = InsertQuery.ExecuteReader()
    IQ.Read()
    LASTID = InsertQuery.Parameters("OUT_LastID").Value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      相关资源
      最近更新 更多