类型' System.InvalidOperationException'的例外情况发生在Microsoft.Practices.EnterpriseLibrary.Data.dll中

时间:2017-12-27 20:28:54

标签: c# asp.net asp.net-mvc oracle stored-procedures

我遇到此错误:类型' System.InvalidOperationException'发生在Microsoft.Practices.EnterpriseLibrary.Data.dll但未在用户代码中处理。附加信息:参数数量与存储过程的值数量不匹配。这是我的代码:

public static IEnumerable<abcProduct> GetAll()
{
    string cnn =
    "database connectionstring";
    using (OracleConnection conn = new OracleConnection(cnn))
    {
        conn.Open();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        cmd.CommandText = "package_name.sp_name";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("resultset_out", OracleDbType.RefCursor, ParameterDirection.Output);
        OracleDataReader rdr = cmd.ExecuteReader();
        //rdr.Read();
        while (rdr.Read())
        {
            yield return new vProduct(GetNullableValue<string>(rdr["ABC_PRODUCT"]), GetNullableValue<string>(rdr["ABC_PRODUCT_AREA"]));
        }
    }
}

这是我的商店程序结构

PROCEDURE PRC_ABC_PRODUCTS_GETALL(resultset_out OUTPUT TYPES.cursorType) AS

BEGIN
    OPEN resultset_out
    FOR

    SELECT UPPER(products.abc_product) abc_product,
        MAX(productareas.abc_product_area) abc_product_area
    FROM SCHEMA.AS_ABC_PRODUCT products
    LEFT JOIN SCHEMA.AS_ABC_PRODUCT_AREA productareas
        ON products.V_PRODUCT_AREA_ID = productareas.ABC_PRODUCT_AREA_ID
    WHERE products.VALID = 1
        AND products.abc_product NOT LIKE '$%'
    GROUP BY UPPER(products.abc_product),
        UPPER(productareas.abc_product_area)
    ORDER BY UPPER(products.ABC_PRODUCT);
END

PRC_ABC_PRODUCTS_GETALL;

有人可以帮忙吗?任何人都能指出错误是什么吗?我们将非常感激一点帮助: - )

1 个答案:

答案 0 :(得分:0)

我在您的代码中看到了几个问题。

  • 应该是cmd.CommandText = "PRC_ABC_PRODUCTS_GETALL";而不是cmd.CommandText = "package_name.sp_name";

  • resultset_out OUTPUT TYPES.cursorType无效的Oracle语法。应该是resultset_out OUT SYS_REFCURSOR。你能编译这个程序吗?

试试这样:

cmd.CommandText = "begin res := PRC_ABC_PRODUCTS_GETALL; end;";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("res", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
OracleDataReader rdr = cmd.ExecuteReader();

和这个PL / SQL:

FUNCTION PRC_ABC_PRODUCTS_GETALL RETURN SYS_REFCURSOR AS
   resultset_out SYS_REFCURSOR;
BEGIN
    OPEN resultset_out
    FOR

    SELECT UPPER(products.abc_product) abc_product,
        MAX(productareas.abc_product_area) abc_product_area
    FROM SCHEMA.AS_ABC_PRODUCT products
    LEFT JOIN SCHEMA.AS_ABC_PRODUCT_AREA productareas
        ON products.V_PRODUCT_AREA_ID = productareas.ABC_PRODUCT_AREA_ID
    WHERE products.VALID = 1
        AND products.abc_product NOT LIKE '$%'
    GROUP BY UPPER(products.abc_product),
        UPPER(productareas.abc_product_area)
    ORDER BY UPPER(products.ABC_PRODUCT);

    RETURN resultset_out;
END;