Oracle软件包的C#问题

时间:2016-08-08 19:59:03

标签: c# oracle packages odp.net procedures

我在Oracle软件包中遇到一个简单的过程问题。我在包中使用的其他程序工作正常。另一个包我们Cursors。这个使用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: 

3 个答案:

答案 0 :(得分:1)

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

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

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

为:

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

可能还有其他问题,但我会从那开始。

答案 1 :(得分:1)

  1. 更改方向&#34; var_Return&#34;到ParameterDirection.Output
  2. 指定&#34; var_Return&#34;的长度。我相信varchar2的默认值是32k(但是我不记得设置的长度如此之大,所以请考虑4k开始)。
  3. 让ODP.net确定输入值的类型(不是要求,但不容易出错IMO)。
  4. 确认您的输入值类型与参数类型
  5. 匹配

    即:

    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;
    

答案 2 :(得分:0)

好吧,我去工作吧。我接受了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之后出现昏迷和字段的长度。