【问题标题】:How to pass an array of strings from C# to an Oracle stored procedure如何将字符串数组从 C# 传递到 Oracle 存储过程
【发布时间】:2020-07-07 00:12:29
【问题描述】:

我有以下代码将字符串表传递给名为 spTest 的 Oracle 存储过程:

using (OracleConnection oracleConnection = new OracleConnection(connectionString))
{
    oracleConnection.Open();
    OracleCommand oracleCommand = new OracleCommand();

    oracleCommand.Parameters.Add(new OracleParameter
    {
        ParameterName = "eventids",
        Direction = ParameterDirection.Input,
        CollectionType = OracleCollectionType.PLSQLAssociativeArray,
        Value = new string[] { "Test1", "Test2" },
        Size = 2,
        UdtTypeName = "T_STRING_TAB"
    });

    oracleCommand.Connection = oracleConnection;
    oracleCommand.CommandText = "spTest";
    oracleCommand.CommandType = CommandType.StoredProcedure;

    using (OracleDataReader oracleDataReader = oracleCommand.ExecuteReader())
    {
        while (oracleDataReader.Read())
        {
            int fieldCount = oracleDataReader.FieldCount;
        }
    }               
}

我在Oracle中已经定义了类型和存储过程如下:

create type T_STRING_TAB is table of varchar2(260) index

create or replace procedure spTest(eventids in T_STRING_TAB)
as
starteventid integer;
begin
starteventid := 1000000;
end;

当我运行代码时,我收到以下错误:

Oracle.ManagedDataAccess.Client.OracleException
HResult=0x80004005
Message=ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SPTEST'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Source=Oracle Data Provider for .NET, Managed Driver

我正在使用 Oracle.ManagedDataAccess.dll 版本号 2.0.18.3。

有谁知道我做错了什么?

谢谢 伊恩

【问题讨论】:

标签: oracle


【解决方案1】:

我在Oracle中已经定义了类型和存储过程如下:

create type T_STRING_TAB is table of varchar2(260) index

这在语法上是无效的,因为它最后有一个额外的 INDEX 关键字,但如果你修复它:

create type T_STRING_TAB is table of varchar2(260);

这是在 SQL 范围内定义的集合数据类型。 C# 目前不支持传递非关联数组。

您的 C# 代码需要一个在 PL/SQL 范围内定义的关联数组:

CollectionType = OracleCollectionType.PLSQLAssociativeArray,

要创建一个 PL/SQL 关联数组,您需要在包内进行并包含 INDEX BY <numeric data type> 子句:

CREATE PACKAGE package_name AS
  TYPE STRING_MAP IS TABLE OF VARCHAR2(260) INDEX BY BINARY_INTEGER;
END;
/

那么你的程序可以是:

create or replace procedure spTest(
  eventids in PACKAGE_NAME.STRING_MAP
)
as
  starteventid integer;
begin
  starteventid := 1000000;
end;
/

【讨论】:

  • 太棒了!非常感谢
猜你喜欢
  • 2012-03-30
  • 2011-07-29
  • 1970-01-01
  • 2012-09-24
  • 2011-01-22
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多