开发者

Invalid parameter type between C# and Oracle

I have a function in Oracle that returns a sequence number. When I开发者_如何学Go try to get that value in C# by a parameter it always tells me that the parameter type is invalid. 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? If it isn't correct then what is the problem?

Oracle function is:

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:

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. 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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜