2010-06-01 176 views
1

Oracle包功能我有一个Oracle包内定义的函数:呼叫使用ODBC从C#

CREATE OR REPLACE PACKAGE BODY TESTUSER.TESTPKG as 
    FUNCTION testfunc(n IN NUMBER) RETURN NUMBER as 
    begin 
    return n + 1; 
    end testfunc; 
end testpkg; 
/

我怎样才能把它从使用ODBC C#?我试过以下内容:

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

class Program { 
    static void Main(string[] args) { 
     using (OdbcConnection connection = new OdbcConnection("DSN=testdb;UID=testuser;PWD=testpwd")) { 
      connection.Open(); 

      OdbcCommand command = new OdbcCommand("TESTUSER.TESTPKG.testfunc", connection); 
      command.CommandType = System.Data.CommandType.StoredProcedure; 

      command.Parameters.Add("ret", OdbcType.Int).Direction = ParameterDirection.ReturnValue; 

      command.Parameters.Add("n", OdbcType.Int).Direction = ParameterDirection.Input; 
      command.Parameters["n"].Value = 42; 

      command.ExecuteNonQuery(); 
      Console.WriteLine(command.Parameters["ret"].Value); 
     } 
    } 
} 

但是我收到一个异常说“无效的SQL语句”。
我在做什么错?

回答

3

在过去,我会使用类似下列的命令字符串:

“{?= CALL JF_TESTUSER.TESTPKG.testFunc(?)}”

请参阅以下article了解更多信息

+0

这也适用,谢谢。 – 2010-06-01 12:39:04

2

尝试

OdbcCommand command = new OdbcCommand("begin ? := TESTUSER.TESTPKG.testfunc(?) end;", connection); 
1

我设法打电话给这样的包装功能:

command.CommandText = @"begin 
    :ret := ILMTEST.testpkg.testfunc(:n); 
end;"; 
command.CommandType = System.Data.CommandType.Text; 
0

我认为你应该考虑使用Oracle Client来代替。

如果您选择ODBC只是为了创建一个DSN,然后连接到它以某种方式与数据库不可知,请考虑使用Enterprise LibraryData Access Application Block

+0

我想避免使用Oracle客户端来避免部署额外的程序集;使用Oracle Client的原因是什么? – 2010-06-01 13:41:14

+0

首先,它可以轻松处理Oracle Package功能。其次,它针对Oracle进行了优化,因此在访问数据时可以获得更好的性能。第三,因为即使微软也不赞成其作为GAC一部分的System.Data.OracleClient提供商,因此,如果我们可以这么说,那么认识到“ODP”是最好的。 – 2010-06-01 13:45:57

+0

我在这里没有使用System.Data.OracleClient提供程序,并且我设法做到了我需要的东西。我应该检查性能,但暂时看起来Odbc对我来说已经足够了... – 2010-06-01 15:04:29