MySqlCommand call function

好久不见. 提交于 2019-12-14 03:53:36

问题


I am using the MySQL Connector.

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);
}

For some reason the returned valus is null. Am I using the right CommandType?

How I can call a MySQL function from .NET?

The final working version is:

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);
    }

回答1:


Add an additional parameter to the command with a parameter direction of:

System.Data.ParameterDirection.ReturnValue;

And use

command.ExecuteNonQuery(); 

And then retrieve the return value from the return value parameter after calling ExecuteNonQuery.



来源:https://stackoverflow.com/questions/3080940/mysqlcommand-call-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!