代码之家  ›  专栏  ›  技术社区  ›  Paolo Tedesco

用C语言中的Odbc调用Oracle包函数#

  •  1
  • Paolo Tedesco  · 技术社区  · 15 年前

    我在Oracle包中定义了一个函数:

    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语句”。
    我做错什么了?

    4 回复  |  直到 15 年前
        1
  •  3
  •   Garett    15 年前

    在过去,我会对命令字符串使用如下内容:

    "{? = 调用JF\u TESTUSER.TESTPKG.testFunc(?)}”

    请参见以下内容 article

        2
  •  2
  •   Erich Kitzmueller    15 年前

    尝试

    OdbcCommand command = new OdbcCommand("begin ? := TESTUSER.TESTPKG.testfunc(?) end;", connection);
    
        3
  •  1
  •   Paolo Tedesco    15 年前

    我设法这样调用包函数:

    command.CommandText = @"begin
        :ret := ILMTEST.testpkg.testfunc(:n);
    end;";
    command.CommandType = System.Data.CommandType.Text;
    
        4
  •  -1
  •   Will Marcouiller    15 年前

    我认为你应该考虑使用 Oracle Client 相反。

    DSN 然后连接到它以某种方式不受数据库的影响,考虑使用 Enterprise Library Data Access Application Block