【问题标题】:Oracle stored procedure gets executed. DataSet returns nothing from vb.netOracle 存储过程被执行。 DataSet 从 vb.net 不返回任何内容
【发布时间】:2018-10-29 05:17:46
【问题描述】:

我在 Oracle 和 vb.net 中有以下代码和存储过程。当我执行代码时,数据集没有返回任何内容。

后面的代码:

grvResults.PageSize = DirectCast(Profile.GetPropertyValue("rowcount"), Integer)
grvResults.DataSourceID = "dsResults"

数据源:

    public static DataSet Fetch(string sp, OracleParameter[] inputParams)
    {
        OracleConnection conn = new OracleConnection(System.Configuration.ConfigurationManager.AppSettings.Get("BertConnection"));
     cmd = new OracleCommand(sp, conn);
        cmd.CommandType = CommandType.StoredProcedure;

        if (inputParams != null)
        {
            cmd.Parameters.AddRange(inputParams);
        }
        cmd.Parameters.Add(new OracleParameter("resultset", OracleDbType.RefCursor, ParameterDirection.Output));

        DataSet ds = new DataSet();

    .
    .
    .

存储过程:

CREATE OR REPLACE PROCEDURE BERT."SP_BIA_OBLIGATION_GROUPED" (
p_bfy in varchar2,
p_fy in varchar2,
p_fm in varchar2,
p_appropriation in varchar2,
p_fund in varchar2,
p_fund_center in varchar2,
p_func_area in varchar2,
p_program in varchar2,
p_boc in varchar2,
p_from_inception in char,
p_red_only in char,
resultset out sys_refcursor
)
 AS

 BEGIN

OPEN resultset FOR
select decode(grouping(t.description), 1, 'TOTAL', t.description) as description,
    case when p_from_inception = '0' then sum(t.amount_cfy) else sum(t.amount_inc) end as amount
from mv_bia_obligation_grouped t

          .
          .
          .

    group by grouping sets ((), (t.description))
    HAVING count(*) != 0;

存储过程执行并返回所需的结果,但从 vb.net 调用时没有提供任何数据。在单步执行代码时,我检查了正确的参数值,当我检查数据集时它只有标题没有数据。有谁知道这里发生了什么?

提前致谢。

【问题讨论】:

  • 连接resultset参数绑定到ds数据集的代码在哪里?你说你正在使用vb.net,但这对我来说确实像 C#。 ???
  • 嗨,鲍勃,我有一个混合代码。页面有 vb 代码,类有 C#。感谢您指出这一点。

标签: vb.net oracle dataset


【解决方案1】:

您的代码绝对是 C# 而不是 vb.net,但这里有一个 vb.net 函数,它返回数据集以调用存储过程。我使用 Oracle 客户端 (Oracle.DataAccess.Client):

'I always provide a open connection to my functions, this way I can execute many procedures on one connection
Public Function fetch(ByVal oraConnection As OracleConnection, ByVal SomeParameter As String) As DataSet
    Dim myCommand As New OracleCommand
    Dim DS As New DataSet
    Try
        With myCommand
            .Connection = oraConnection
            .CommandText = "Bert.SP_BIA_OBLIGATION_GROUPED"
            .CommandType = CommandType.StoredProcedure
            .Parameters.Clear()
             ' Add all your input parameters here ...
            .Parameters.Add(New OracleParameter("SomeParameter", OracleDbType.Varchar2, SomeParameter.Length)).Value = SomeParameter 
            ' This is the resultset output parameter
            .Parameters.Add(New OracleParameter("pDataOut", OracleDbType.RefCursor))
            ' I always give my parameters direction just for clarity and readability, no other reason as the default is input
            .Parameters(0).Direction = ParameterDirection.Input
            .Parameters(1).Direction = ParameterDirection.Output
            Dim DA As New OracleDataAdapter(myCommand)
            DA.Fill(DS, "DATA")
            fetch = DS
        End With
    Catch exc As Exception
        Throw New Exception("Error occurred while retrieving data, the error is: " & exc.Message)
    End Try
    myCommand = Nothing
End Function

C# 代码

public DataSet fetch(OracleConnection oraConnection, string SomeParameter)
{
    OracleCommand myCommand = new OracleCommand();
    DataSet DS = new DataSet();
    try
    {
        {
            var withBlock = myCommand;
            withBlock.Connection = oraConnection;
            withBlock.CommandText = "Bert.SP_BIA_OBLIGATION_GROUPED";
            withBlock.CommandType = CommandType.StoredProcedure;
            withBlock.Parameters.Clear();
            // Add all your input parameters here ...
            withBlock.Parameters.Add(new OracleParameter("SomeParameter", OracleDbType.Varchar2, SomeParameter.Length)).Value = SomeParameter;
            // This is the resultset output parameter
            withBlock.Parameters.Add(new OracleParameter("pDataOut", OracleDbType.RefCursor));
            // I always give my parameters direction just for clarity and readability, no other reason as the default is input
            withBlock.Parameters(0).Direction = ParameterDirection.Input;
            withBlock.Parameters(1).Direction = ParameterDirection.Output;
            OracleDataAdapter DA = new OracleDataAdapter(myCommand);
            DA.Fill(DS, "DATA");
            fetch = DS;
        }
    }
    catch (Exception exc)
    {
        throw new Exception("Error occurred while retrieving data, the error is: " + exc.Message);
    }
    myCommand = null/* TODO Change to default(_) if this is not a reference type */;
}

【讨论】:

  • 感谢 Prescott 的帮助,实际上我有混合代码。页面有 vb.net 和这个函数用 C# 代码编写的类。
  • 我添加了与我的答案等效的 C#。
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 2014-05-28
  • 1970-01-01
相关资源
最近更新 更多