简体   繁体   English

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

[英]Invalid parameter type between C# and Oracle

I have a function in Oracle that returns a sequence number.我在 Oracle 中有一个 function ,它返回一个序列号。 When I try to get that value in C# by a parameter it always tells me that the parameter type is invalid.当我尝试通过参数在 C# 中获取该值时,它总是告诉我参数类型无效。 A sample value of that returned value is 115545. If indeed this error is correct then what is the correspondent in ODBCType for this value?该返回值的示例值为 115545。如果此错误确实是正确的,那么 ODBCType 中该值的对应对象是什么? If it isn't correct then what is the problem?如果不正确,那么问题是什么?

Oracle function is: 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# Code: 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);

I believe the problem is in the way you're setting next_id .我相信问题出在您设置next_id的方式上。 You're first creating an OdbcParameter and setting its direction - but then completely ignoring it, and reassigning the variable using the Add method, which will be creating an "in" parameter by default.您首先创建一个OdbcParameter并设置其方向 - 但随后完全忽略它,并使用Add方法重新分配变量,默认情况下将创建一个“in”参数。 Try this instead:试试这个:

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

Note that you should also be using using statements for the connection and command, to ensure they're cleaned up at the end of the code, whatever happens.请注意,您还应该对连接和命令使用using语句,以确保在代码末尾清除它们,无论发生什么。

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

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