从 C# 将数组传递到 Oracle 过程

问题描述 投票:0回答:1

我正在尝试将数组传递给 oracle 过程。我搜索了它,首先我创建了一个名为“dizi”的类型(就像这里在此处输入链接描述)。所以它可以在oracle开发者中运行。问题是;我无法将我的 C# 数组作为参数传递给过程。那么我如何将数组传递给我的程序?

这是我的代码(当我执行时,oracle 错误说:并非所有变量都绑定)

public void InsertQuestion(List<string> area_list)
{
    quest_areas = area_list.ToArray();
    command.Connection = connect;
    connect.Open();

    var arry = command.Parameters.Add("Areas", OracleDbType.Varchar2);
    arry.Direction = ParameterDirection.Input;
    arry.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
    arry.Value = quest_areas;


    command.CommandText ="TESTPROCEDURE(:Areas)";
    command.CommandType = CommandType.StoredProcedure;
    command.ExecuteNonQuery();
    connect.Close();
}
c# arrays oracle stored-procedures
1个回答
16
投票
  1. 定义数组类型和过程:

    CREATE or replace PACKAGE Testpackage AS 
      TYPE Areas_t is table of VARCHAR(100) index by BINARY_INTEGER;
      PROCEDURE TESTPROCEDURE(Areas IN Areas_t);       
    END Testpackage; 
    
  2. C# 例程:

    public void InsertQuestion(IEnumerable<string> area_list)
    {
        var connect = new OracleConnection("YOUR CONNECTION STRING");
    
        var command = new OracleCommand("BEGIN Testpackage.Testprocedure(:Areas); END;", connect);
    
        connect.Open();
    
        var arry = command.Parameters.Add("Areas", OracleDbType.Varchar2);
    
        arry.Direction = ParameterDirection.Input;
        arry.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
        arry.Value = area_list.ToArray();
        arry.Size = area_list.Count();
        arry.ArrayBindSize = area_list.Select(_ => _.Length).ToArray();
        arry.ArrayBindStatus = Enumerable.Repeat(OracleParameterStatus.Success, area_list.Count()).ToArray();
    
        command.ExecuteNonQuery();
    
        connect.Close();
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.