gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:45:19 26 4
gpt4 key购买 nike

我在使用 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(网络项目,但我将此代码写入 WinForms 应用程序并遇到同样的问题),oracle.dataaccess.dll 4.112.3.0

最佳答案

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

        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();

可能对某人有用。

关于c# - 使用 OracleDataReader (ODAC) 返回 Oracle 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com