什么是调用Oracle存储过程与净参数正确的ODBC命令?存储过程、命令、正确、参数

2023-09-03 01:19:25 作者:对着太阳喊声日

在MSFT SQL Server的08的情况下,它是:

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.

随时要求澄清,并请帮助。我使用.net 3.5,SQL Server的08和Oracle 11g_home1。

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

P.S。 Oracle的存储过程确实有一些3个参数,但我相信,我在我的code处理这一点。

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

推荐答案

我在运行.NET 3.5和Oracle 10gR2中。我回应您的评论增加了一个输出参数。

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

服务器的Uid PWD 已变更为保护无辜者。它们设置为相应的值。

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

我的存储过程:

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

我的测试code:

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
        }
    }
}

在使用 OUT IN OUT 参数,尺寸财产 OdbcParameter 必须设置为一个适当的值。

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.