繁体   English   中英

C# 和 Oracle 之间的参数类型无效

[英]Invalid parameter type between C# and Oracle

我在 Oracle 中有一个 function ,它返回一个序列号。 当我尝试通过参数在 C# 中获取该值时,它总是告诉我参数类型无效。 该返回值的示例值为 115545。如果此错误确实是正确的,那么 ODBCType 中该值的对应对象是什么? 如果不正确,那么问题是什么?

Oracle function 是:

FUNCTION Get_Next_Message_Id__ 
RETURN NUMBER IS

  temp_ NUMBER;
  CURSOR get_in_message_id_seq IS
    SELECT in_message_id_seq.NEXTVAL
      FROM dual;

BEGIN

   General_SYS.Init_Method(lu_name_, 'IN_MESSAGE_API', 'Get_Next_Message_Id__', TRUE);

   OPEN get_in_message_id_seq;
   FETCH get_in_message_id_seq INTO temp_;
   CLOSE  get_in_message_id_seq;

   RETURN(temp_);

END Get_Next_Message_Id__;

C# 代码:

OdbcConnection myConnection = new OdbcConnection(ODBC_CLass.ifsconnectionstring);
OdbcParameter next_id = new OdbcParameter();
next_id.Direction = ParameterDirection.Output;

myConnection.Open();
OdbcCommand command = new OdbcCommand("{? = call ifsapp.in_message_api.Get_Next_Message_Id__}", myConnection);
command.CommandType = CommandType.StoredProcedure;
next_id = command.Parameters.Add("temp_", OdbcType.Int);
int k = command.ExecuteNonQuery();
MessageBox.Show("next_id: " + next_id.Value);

我相信问题出在您设置next_id的方式上。 您首先创建一个OdbcParameter并设置其方向 - 但随后完全忽略它,并使用Add方法重新分配变量,默认情况下将创建一个“in”参数。 试试这个:

OdbcParameter nextId = command.Parameters.Add("temp_", OdbcType.Int);
nextId.Direction = ParameterDirection.Output;

请注意,您还应该对连接和命令使用using语句,以确保在代码末尾清除它们,无论发生什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM