views:

645

answers:

2

How from ASP.NET I can call a stored procedure in an Oracle package that accepts a VArray. The VArray will pass a list of keys that the stored procedure will use to modify appropriate records. I know I could send the keys in separate calls or send a delimited list, but I'd rather use an array.

A: 

Did you try this

OracleCommand cmd = OracleConnection1.CreateCommand();
cmd.CommandText = "INSERT INTO ArrayTable VALUES (:CODE, :TITLE, : ARR1, :ARR2)";
...
OracleArray arr1 = new OracleArray("SCOTT.TARRAY1", OracleConnection1);
arr1.Add(10);
arr1.Add(20);
arr1.Add(30);
...
cmd.Parameters["ARR1"].DbType = OracleDbType.Array;
cmd.Parameters["ARR1"].Value = arr1;
...
cmd.ExecuteNonQuery();
Craig
No I haven't tried it, nor would it meet my requirements as it inserts directly into the table rather than calling the stored procedure (which may do more than just a simple insert). It looks like you are referencing an Oracle array type (SCOTT.TARRAY1), so that does get me part way there.
Leigh Riffel
I didn't notice the link initially. This solution also requires purchasing dotConnect from devart.
Leigh Riffel
+1  A: 

Assuming you're using ODP.NET (you shouldn't be using System.Data.OracleClient anyway), here's how to do it:

using System;
using System.Data;
using Oracle.DataAccess.Client;

class SomeClass
{
    void SomeMethod(string connectionString, int[] anArrayOfKeys)
    {
        using (var con = new OracleConnection(connectionString))
        using (var cmd = con.CreateCommand())
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "StoredProcedureNameGoesHere";
            cmd.Parameters.Add(
                "ParameterNameGoesHere",
                OracleDbType.Array,
                anArrayOfKeys,
                ParameterDirection.Input);

            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
}
Vadim K.