使用OracleDataReader(ODAC)返回Oracle存储过程

时间:2013-06-28 06:00:32

标签: c# odp.net odac

使用OracleDataReader(Oracle.DataAccess.Client)来自Oracle存储过程的输出数据存在问题。

步骤:

    procedure LOAD_BL_REQ_2(P_XML CLOB, P_XML_OUT out CLOB) is
    BEGIN

     P_XML_OUT    := 'TEST1111';

     exception
     when others then
        P_XML_OUT   := 'LOAD_BL_REQ: Error'|| SQLERRM;
    END;

C#代码:

    OracleCommand cmd = new OracleCommand();
    cmd.Connection = OraConnection;
    cmd.CommandText = "IBS.BNT_EQ.LOAD_BL_REQ_2";
    cmd.CommandType = System.Data.CommandType.StoredProcedure;

    OracleParameter result = new OracleParameter();
    result.ParameterName = "P_XML_OUT";
    result.OracleDbType = OracleDbType.Clob;
    result.Direction = System.Data.ParameterDirection.Output;
    cmd.Parameters.Add(result);

    OracleParameter XMLString = new OracleParameter();
    XMLString.ParameterName = "P_XML";
    XMLString.OracleDbType = OracleDbType.Varchar2;
    XMLString.Direction = System.Data.ParameterDirection.Input;

    OracleDataReader dr;

    cmd.Transaction = OraConnection.BeginTransaction();
    try
    {
        XMLString.Value = XML;
        cmd.Parameters.Add(XMLString);
        dr = cmd.ExecuteReader();
        cmd.Transaction.Commit();
    }
    catch (OracleException ex)
    {
        cmd.Transaction.Rollback();
        Log(2, "Transaction fail, exception: " + ex.ToString());
        ORADisconnect();
        return "";
    }

在调试过程中,我看到了dr:

    Depth: 0
    FetchSize: 131072
    FieldCount: 0
    HasRows: false

对于测试我添加计数代码:

    while (dr.Read())
    {
        count++;
    }

任何时候计数都是0。

此代码(例如):

  if(dr.IsDBNull(0))
  {
   //etc...
  }

  OracleClob oclob;
  oclob = dr.GetOracleClob(0);

返回异常:

  dr.isdbnull exception: System.InvalidOperationException: Operation is not valid due to the current state of the object.
  at Oracle.DataAccess.Client.OracleDataReader.IsDBNull(Int32 i)
  at GlobalFunc.ORA_BlackList_Test(String XML) in c:\inetpub\project\App_Code\GlobalFunc.cs:line 474

在最后,我需要从Oracle SP检索CLOB数据,但现在已停止在此步骤。 使用:Visual Studio 2012,ASP.Net(Web项目,但我将此代码写入WinForms应用程序并遇到同样的问题),oracle.dataaccess.dll 4.112.3.0

3 个答案:

答案 0 :(得分:3)

我找到了解决方案!但是,一切都很简单:

        string XML = "XML Data";
        OracleCommand cmd = OraConnection.CreateCommand();

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "IBS.BNT_EQ.LOAD_BL_REQ_2";

        OracleParameter result = new OracleParameter();
        result.ParameterName = "P_XML";
        result.OracleDbType = OracleDbType.Clob;
        result.Value = XML;
        result.Direction = System.Data.ParameterDirection.InputOutput;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.ToString());
        }

        string str = (result.Value as OracleClob).Value;
        MessageBox.Show("Val: " + str);

        OraConnection.Close();

可能对某人有用。

答案 1 :(得分:1)

您必须致电dataReader.Read()以实际从DataReader获取一些数据。

另请注意,ODP.NET默认忽略参数名称并按位置绑定。这意味着在您的例子中,参数是错误的。创建命令后调用cmd.BindByName = true或以正确的顺序将它们添加到命令的Parameters集合。

答案 2 :(得分:0)

Procedure view,, 谢谢,跑!

 protected void Save(object sender, EventArgs e)
    {
        OracleConnection conn = new OracleConnection("Data Source=XE;Persist Security Info=True;Password=*****;User ID=WINCELL");
        string procedure = "WINCELL_API.ADD_CAMPAIGN";
        OracleCommand cmd = new OracleCommand(procedure, conn);
       // DataTable dt = new DataTable();
       // OracleDataAdapter da = new OracleDataAdapter();

        string xml = "<campaign>";
        xml += "\n";
        xml += "<campaignInfo>";
        xml += "\n";
        xml += "<name>" + TextBox1.Text + "</name>";
        xml += "\n";
        xml += "<startDate>" + TextBox2.Text + "</startDate>";
        xml += "\n";
        xml += "<period>" + TextBox3.Text + "</period>";
        xml += "\n";
        xml += "<handsetStatu>" + TextBox4.Text + "</handsetStatu>";
        xml += "\n";
        xml += "<ServiceID>" + TextBox5.Text + "</ServiceID>";
        xml += "\n";
        xml += "</campaignInfo>";
        xml += "\n";
        xml += "</campaign>";


        TextBox6.Text = xml;

            conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            OracleParameter param = new OracleParameter();
            param.ParameterName = "";
            param.OracleDbType = OracleDbType.Clob;
            param.Value = xml;
            param.Direction = System.Data.ParameterDirection.InputOutput;
            cmd.Parameters.Add(param);
            try
            {
            cmd.ExecuteNonQuery(); 
            }
            catch (Exception ex)
            {
            Label1.Text = error" + ex.ToString();

            }
        //string str = (param.Value as OracleClob).Value;
        //Label1.Text = "val: " + str;
    }