【问题标题】:MySqlCommand call functionMySqlCommand 调用函数
【发布时间】:2010-06-20 21:06:37
【问题描述】:

我正在使用 MySQL 连接器。

using (MySqlConnection connection = new MySqlConnection("..."))
{
    connection.Open();
    MySqlCommand command = new MySqlCommand();
    command.Connection = connection;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "FN_NEW";
    command.Parameters.AddWithValue("P_SESSION_ID", sessionId);
    command.Parameters.AddWithValue("P_NAME", deckName);
    object result = command.ExecuteScalar(); // returns NULL !!!!!!!!!
    return Json(result);
}

由于某种原因,返回值为空。我是否使用了正确的 CommandType?

如何从 .NET 调用 MySQL 函数?

最终的工作版本是:

using (MySqlConnection connection = new    MySqlConnection(GetConnectionString().ConnectionString))
    {
        connection.Open();
        MySqlCommand command = new MySqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "FN_NEW";
        command.Parameters.AddWithValue("P_SESSION_ID", sessionId);
        command.Parameters.AddWithValue("P_NAME", deckName);
        MySqlParameter returnParam = command.Parameters.Add("@RETURN_VALUE", MySqlDbType.Int32);
        returnParam.Direction = System.Data.ParameterDirection.ReturnValue;            
        command.ExecuteNonQuery();
        NewDeckReturnCode result = (NewDeckReturnCode)returnParam.Value;
        return Json(result);
    }

【问题讨论】:

    标签: .net mysql function return-value call


    【解决方案1】:

    在命令中添加一个附加参数,参数方向为:

    System.Data.ParameterDirection.ReturnValue;
    

    并使用

    command.ExecuteNonQuery(); 
    

    然后在调用ExecuteNonQuery后从返回值参数中取出返回值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-20
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多