的MySqlCommand通话功能 [英] MySqlCommand call function
本文介绍了的MySqlCommand通话功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的MySQL连接。
使用(的MySqlConnection连接=新的MySqlConnection(...))
{
connection.Open();
的MySqlCommand命令=新的MySqlCommand();
command.Connection =连接;
command.CommandType = CommandType.StoredProcedure;
command.CommandText =FN_NEW;
command.Parameters.AddWithValue(P_SESSION_ID的sessionId);
command.Parameters.AddWithValue(P_NAME,deckName);
对象result = command.ExecuteScalar(); //返回NULL !!!!!!!!!
返回JSON(结果);
}
由于某种原因,返回的衣被合计为null。我使用正确的CommandType?
我如何从.NET调用MySQL的函数?
最后一个工作版本是:
使用(的MySqlConnection连接=新的MySqlConnection(GetConnectionString()的ConnectionString))
{
connection.Open();
的MySqlCommand命令=新的MySqlCommand();
command.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();
NewDeckReturn code结果=(NewDeckReturn code)returnParam.Value;
返回JSON(结果);
}
解决方案
添加一个额外的参数给命令的参数方向:
System.Data.ParameterDirection.ReturnValue;
和使用
command.ExecuteNonQuery();
,然后调用的ExecuteNonQuery后检索的返回值参数的返回值。
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);
}
解决方案
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.
这篇关于的MySqlCommand通话功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文