views:

425

answers:

1

In the case of MSFT SQL Server 08, it is:

odbcCommand = new OdbcCommand("{call " + SP_NAME + " (?,?,?,?,?,?,?) }", odbcConn);

When I try to do the same thing for Oracle, I get:

OdbcException: ERROR [HYC00] [Oracle][ODBC]Optional feature not implemented.

Feel free to ask for clarification, and please help. I am using .Net 3.5, SQL Server 08, and Oracle 11g_home1.

P.S. The Oracle stored procedure does have some 3 more parameters, but I believe I am handling this in my code.

+1  A: 

I'm running .NET 3.5 and Oracle 10gR2. I've added an output parameter in response to your comment.

Server, Uid, and Pwd have been changed to protect the innocent. Set them to appropriate values.

My stored procedure:

create or replace
procedure test_proc(p1 in number, p2 in out varchar2) is
begin
  p2 := to_char(p1 + to_number(p2));
end;

My test code:

using System;
using System.Data;
using System.Data.Odbc;

class OdbcTest
{
    const string connectionString =
        @"Driver={Microsoft ODBC for Oracle};" +
        @"Server=MyTnsName;" +
        @"Uid=MySchema;" +
        @"Pwd=MyPassword;";

    public static string TryMe()
    {
        using (var odbcConn = new OdbcConnection(connectionString))
        using (var odbcCommand = odbcConn.CreateCommand())
        {
            odbcCommand.CommandText = "{ CALL test_proc(?, ?) }";
            odbcCommand.CommandType = CommandType.StoredProcedure;

            odbcCommand.Parameters.Add("p1", OdbcType.Decimal).Value = 42;
            var p2 = odbcCommand.Parameters.Add("p2", OdbcType.VarChar, 30);
            p2.Direction = ParameterDirection.InputOutput;
            p2.Value = "25";

            odbcConn.Open();
            odbcCommand.ExecuteNonQuery();

            return p2.Value as string; // returns 67
        }
    }
}

When using OUT or IN OUT parameters, the Size property of the OdbcParameter must be set to an adequate value.

In response to your comment regarding exception handling, I would have the caller of the data access method handle exceptions. With the using construct, the Dispose method will be called automatically on the command and connection objects whether there is an exception or not.

Vadim K.
Also, where would I be able to specify the parameter type and the direction: in, out, inout? Oracle is pretty picky about that. Thanks.
Hamish Grubijan
I've updated my solution, please have a look.
Vadim K.
Great! However imagine that this query is called from an ASP.Net page, and the parameters come in through the GET url. If parameters are wrong, or if the stored procedure does not run correctly, I need to write that information back to the user (in plain ASCII). I imagine I would need to catch some Exceptions manually. How would that work together with a "using"? Or ... would I have to call Dispose() myself?
Hamish Grubijan
By the way, any luck with `in out` parameter? That is what has created much havoc for me.
Hamish Grubijan
Solution updated to demonstrate `in out` and added comment on exception handling. As for ASP.NET, that should be a separate question.
Vadim K.