ODP.NET Oracle.DataAccess ArrayBindCount OUT数组参数异常处理

时间:2011-06-20 15:11:46

标签: arrays oracle exception-handling odp.net data-binding

我的情景:

        helper = new OracleHelper();
        helper.CreateAndOpenConnection();
        //cmd = new OracleCommand("PCK_JOBS.ARRAY_INSERT", helper.OracleHelperConnection);
        cmd = new OracleCommand("PCK_JOBS.SCALAR_INSERT", helper.OracleHelperConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.BindByName = true;
        cmd.ArrayBindCount = 3;

        var paramNames = new OracleParameter();
        paramNames.ParameterName = "P_JOB_TITLE";
        paramNames.Size = 3;
        paramNames.OracleDbType = OracleDbType.Varchar2;
        paramNames.Value = new string[3] { "1", "2", "3" };
        cmd.Parameters.Add(paramNames);

        paramNames = new OracleParameter();
        paramNames.ParameterName = "O_JOB_ID";
        paramNames.Size = 3;
        paramNames.Direction = ParameterDirection.Output;
        paramNames.OracleDbType = OracleDbType.Int32;
        paramNames.Value = new int[3] { 0, 0, 0 };

        cmd.Parameters.Add(paramNames);

        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception exc)
        {
        }
        var outParams = cmd.Parameters[1];

当PL-SQL过程失败例如插入第2行,因此ExecuteNonQuery catch时,我的输出参数(outParams)都被设置为0(初始值),也适用于由过程正确处理的行。

有没有办法正确处理这个?所以正确填充部分OUT数组参数?

2 个答案:

答案 0 :(得分:0)

这是我在另一个帖子中所指的;处理pl / sql中的异常:

create or replace
PROCEDURE TESTDATA(myArrayNo in number , myOutputArray out varchar2)
is
    customException EXCEPTION;
    PRAGMA EXCEPTION_INIT( customException, - 20101 );
begin
    myOutputArray := chr( myArrayNo );
    if mod( myArrayNo, 2 ) = 0 then
        raise customException;
    end if;

exception
    when customException then
         myOutputArray := 'I am invalid';
end TESTDATA;

但是这里只捕获预期的错误,你不想停止进展的错误,如果你把其他人放进去......那么事情可能变得毛茸茸并隐藏错误并允许批量更新/插入你不能我想允许。

c#c​​ode:


...
      int[]    myArrayNo   = new int[3]{65, 66, 67};
      String[] myOutputArray ;


      OracleConnection connection = new OracleConnection(connectStr);
      OracleCommand    command    = new OracleCommand (
        "TESTDATA", connection);
          //insert into dept values (:deptno, :deptname, :loc)
      // Set the Array Size to 3. This applied to all the parameter in 
      // associated with this command
      command.ArrayBindCount = 3;
        command.CommandType = CommandType.StoredProcedure  ;
      command.BindByName = true;

      // deptno parameter
      OracleParameter arrayNoParam = new OracleParameter("myArrayNo",OracleDbType.Int32);
      arrayNoParam.Direction       = ParameterDirection.Input;
      arrayNoParam.Value           = myArrayNo;
      command.Parameters.Add(arrayNoParam);


      OracleParameter arrayOutParam = new OracleParameter();
      arrayOutParam.ParameterName = "myOutputArray" ;
      arrayOutParam.Direction       = ParameterDirection.Output ;
      arrayOutParam.ArrayBindSize   = new int[3] { 50, 50, 50 };
      arrayOutParam.OracleDbTypeEx  = OracleDbType.Varchar2  ;
      command.Parameters.Add(arrayOutParam);

      try 
      {
        connection.Open();
        command.ExecuteNonQuery();
        myOutputArray = (String[])command.Parameters["myOutputArray"].Value ;

                for (int i = 0; i < 3; i++){
                  Console.WriteLine("myOutputArray{0} = {1}", 
                              i, myOutputArray[i]);
              }                 
      }
        catch (OracleException e)
            {
              Console.WriteLine("OracleException {0} occured", e.Message);
              if (e.Number == 24381)
                for (int i = 0; i < e.Errors.Count; i++){
                  Console.WriteLine("Array Bind Error {0} occured at Row Number {1}", 
                    e.Errors[i].Message, e.Errors[i].ArrayBindIndex);
              }
            }

和输出

 myOutputArray0 = A 
 myOutputArray1 = I am invalid 
 myOutputArray2 = C

答案 1 :(得分:0)

您也可以使用此

cmd.ArrayBindCount = length;

                    Oracle.DataAccess.Client.OracleParameter P_FinVslCd = new Oracle.DataAccess.Client.OracleParameter("ic_fin_vsl_code", Oracle.DataAccess.Client.OracleDbType.Varchar2);
                    P_FinVslCd.Direction = ParameterDirection.Input;
                    P_FinVslCd.Value = ArrFinVslCd;
                    cmd.Parameters.Add(P_FinVslCd);