使用 .Net 中的参数调用 Oracle 存储过程的正确 odbc 命令是什么?

发布于 2024-08-29 14:15:07 字数 405 浏览 2 评论 0原文

对于 MSFT SQL Server 08,它是:

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

当我尝试对 Oracle 执行相同的操作时,我得到:

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

请随时要求澄清,并请提供帮助。我正在使用.Net 3.5、SQL Server 08 和 Oracle 11g_home1。

PS Oracle 存储过程确实还有另外 3 个参数,但我相信我正在代码中处理这个参数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-09-05 14:15:07

我正在运行 .NET 3.5 和 Oracle 10gR2。我添加了一个输出参数来响应您的评论。

ServerUidPwd 已更改以保护无辜者。将它们设置为适当的值。

我的存储过程:

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

我的测试代码:

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

使用 OUTIN OUT 参数时,OdbcParameterSize 属性必须设置为足够的值。

为了回应您有关异常处理的评论,我会让数据访问方法的调用者处理异常。使用 using 构造,无论是否存在异常,都将在命令和连接对象上自动调用 Dispose 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文