簡體   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