8

I'm trying to pass an array to oracle procedure. I searched about it and firstly i created a type named 'dizi' (like here enter link description here). So it works in oracle developer. The problem is; i can't pass my c# array to procedure as a parameter. So how can i pass my array to my procedure?

Here is my code (When i execute, oracle error says: Not all variables bound)

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();
}
7
  • You are already doing it command.Parameters.Add adding the DbParameter, internally OracleParameter Commented Oct 27, 2016 at 8:01
  • But it doesn't work? Commented Oct 27, 2016 at 8:12
  • What's the error, can you check ODP.Net sample Commented Oct 27, 2016 at 8:14
  • I think you missed arry.Size = quest_areas.Length; Commented Oct 27, 2016 at 8:16
  • @MrinalKamboj Error is:Not all variables bound Commented Oct 27, 2016 at 8:22

1 Answer 1

16
  1. Define an array type and a procedure:

    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# routine:

    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();
    }
    
Sign up to request clarification or add additional context in comments.

3 Comments

Would you be kind enough to enrich this example so as to show how to use 'Areas' inside the implementation of TESTPROCEDURE? Thank you in advance.
seems like you don't need a bunch of parameters like Size,Direction, ArrayBindSize, ArrayBindStatus?
Side note: IEnumerable.ToArray() requires System.Linq.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.