Oracle包的c#问题

本文关键字:问题 包的 Oracle | 更新日期: 2023-09-27 18:01:18

我在Oracle包中有一个简单过程的问题。我在包中使用的其他程序工作得很好。其他包使用游标。这个使用Return。我无法获得通过cmd.ExecuteNonQuery()的代码。所以我的想法是我没有正确地构建命令。我试着修改命令,但它似乎仍然不起作用。如果有人能看一下,我会很感激的。

Oracle包中的存储过程:

Name             Data Types:
ARC_NUMBER       VARCHAR2(12)
TRANSACTION_ID   NUMBER
TRANS_STATUS     VARCHAR2(1)
PAID_AMT         NUMBER(8,2)
PROCEDURE UPDATE_TRANS_BY_OUTPUT(
var_ArcNum IN TRANSACTION.ARC_NUMBER%type,
var_Trans IN TRANSACTION.TRANSACTION_ID%type,
var_TransStatus IN TRANSACTION.TRANS_STATUS%type,
var_PaidAmt IN TRANSACTION.PAID_AMT%type,
var_Return OUT VARCHAR2)   
IS BEGIN 
BEGIN
UPDATE TRANSACTION
SET
  ARC_NUMBER = var_ArcNum,
  TRANS_STATUS = var_TransStatus,
  PAID_AMT = var_PaidAmt
WHERE TRANSACTION_ID = var_Trans;
  var_Return := 'PASS';
EXCEPTION
WHEN OTHERS THEN
  var_Return := 'FAIL';
END UPDATE_TRANS_BY_DEDUCT;

c#代码

    public void updateTransactionDeduct(string txtArc, string txtTransID, string vStatus, string txtPaidAmt)
    {
        string constr = WebConfigurationManager.ConnectionStrings["OracleDbConnection"].ConnectionString;
        OracleConnection con = new OracleConnection(constr);
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "ucs.trans_data.UPDATE_TRANS_BY_OUTPUT";
        cmd.BindByName = true;
        //ASSIGN PARAMETERS TO BE PASSED
        cmd.Parameters.Add("var_ArcNum", OracleDbType.Varchar2).Value = txtArc;
        cmd.Parameters["var_ArcNum"].Direction = ParameterDirection.Input;
        cmd.Parameters.Add("var_Trans", OracleDbType.Int16).Value = txtTranIDNum;
        cmd.Parameters["var_Trans"].Direction = ParameterDirection.Input;
        cmd.Parameters.Add("var_TransStatus", OracleDbType.Varchar2).Value = vStatus;
        cmd.Parameters["var_TransStatus"].Direction = ParameterDirection.Input;
        cmd.Parameters.Add("var_PaidAmt", OracleDbType.Decimal).Value = txtPaidAmtNum;
        cmd.Parameters["var_PaidAmt"].Direction = ParameterDirection.Input;
        cmd.Parameters.Add("var_Return", OracleDbType.Varchar2);
        cmd.Parameters["var_Return"].Direction = ParameterDirection.ReturnValue;
        con.Open();
        cmd.ExecuteNonQuery(); //<=== Errors Out Here
        string txtReturn = cmd.Parameters["var_Return"].Value.ToString();
        con.Close();
        if (txtReturn == "FAIL" | txtReturn == null)
        {
            MsgBox("Transaction record not updated.");
        }
        else
        {
            MsgBox("Transaction was updated.");
        }
    }

当我将异常细节复制到剪贴板时,我得到的是

剪贴板异常详细信息

Oracle.ManagedDataAccess.Client.OracleException was unhandled by user code
  DataSource=""
  ErrorCode=-2147467259
  HResult=-2147467259
  IsRecoverable=false
  Message=ORA-06550: line 1, column 15:
PLS-00306: wrong number or types of arguments in call to 'UPDATE_TRANS_BY_DEDUCT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
  Number=6550
  Procedure=""
  Source=Oracle Data Provider for .NET, Managed Driver
  StackTrace:
   at OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl, Int32& cursorId, Boolean bThrowArrayBindRelatedErrors, OracleException& exceptionForArrayBindDML, Boolean& hasMoreRowsInDB, Boolean bFirstIterationDone)
   at OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl, Int32& cursorId, Boolean bThrowArrayBindRelatedErrors, OracleException& exceptionForArrayBindDML, Boolean bFirstIterationDone)
   at OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteNonQuery(String commandText, OracleParameterCollection paramColl, CommandType commandType, OracleConnectionImpl connectionImpl, Int32 longFetchSize, Int64 clientInitialLOBFS, OracleDependencyImpl orclDependencyImpl, Int64[]& scnFromExecution, OracleParameterCollection& bindByPositionParamColl, Boolean& bBindParamPresent, OracleException& exceptionForArrayBindDML, Boolean isFromEF)
   at Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteNonQuery()
   at Query_Test.Review_List.updateTransactionDeduct(String txtPolicy, String txtTransID, String vStatus, String txtPaidAmt) in C:'Working'Test'Query_Test'Query_Test'Review_List.aspx.cs:line 314
   at Query_Test.Review_List.GridViewListComp_RowUpdated(Object sender, GridViewUpdateEventArgs e) in C:'Working'Test'Query_Test'Query_Test'Review_List.aspx.cs:line 115
   at System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e)
   at System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation)
   at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
   at System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

Oracle包的c#问题

从技术上讲,var_Return不是函数的返回值,而是一个输出参数。

出于这个原因,我会尝试修改:

cmd.Parameters["var_Return"].Direction = ParameterDirection.ReturnValue;

:

cmd.Parameters["var_Return"].Direction = ParameterDirection.Output;

可能还有其他问题,但我想从这个开始。

  1. 将"var_Return"的方向更改为ParameterDirection。输出
  2. 指定"var_Return"的长度。我相信varchar2的默认值是32k(但我不记得曾经设置过这么大的长度,所以考虑4k开始)。
  3. 让ODP.net确定输入值的类型(不是要求,但IMO不容易出错)。
  4. 确认输入值类型与参数类型匹配
ie:

cmd.Parameters.Add("var_ArcNum",  txtArc);
cmd.Parameters.Add("var_Trans", Int32.Parse(txtTranIDNum));//if this is a string, you have to convert it in order to pass it as an int, even if you specify the parameter type
cmd.Parameters.Add("var_TransStatus", vStatus);
cmd.Parameters.Add("var_PaidAmt", Decimal.Parse(txtPaidAmtNum));
cmd.Parameters.Add("var_Return", OracleDbType.Varchar2, 4000);
cmd.Parameters["var_Return"].Direction = ParameterDirection.Output;

我开始工作了。我接受了sstan的建议并将ReturnValue切换到Output。我还为Varchars和Return添加了字段长度。它看起来像这样

//                   Added the 12 after the Varchar, ==='/
cmd.Parameters.Add("var_ArcNum", OracleDbType.Varchar2, 12).Value = txtArc; 
//                   Added the 4 after the Varchar, ==='/
cmd.Parameters.Add("var_Return", OracleDbType.Varchar2, 4); 

在Varchar和字段长度后面加一个逗号