4

我有一个使用 ado 连接到 Oracle 数据库的 vb.net 代码库。我们有很多我们调用的存储过程,其中一些带有多个输出参数。但是,我现在需要调用一个存储函数,我不清楚如何将函数的结果返回到我的 VB 代码中。

编辑:我返回一个整数。

如何从 ado.net 正确调用 oracle 存储函数?

4

2 回答 2

7

我假设您使用的是 ODP.net(.net 的本机 Oracle 客户端)。

假设您有 2 个这样的 Oracle 存储函数:

   FUNCTION my_func
   (
      p_parm1 VARCHAR2
    , p_parm2 NUMBER
   ) RETURN VARCHAR2
   AS
   BEGIN
      RETURN p_parm1 || to_char(p_parm2);
   END;

   FUNCTION my_func2 RETURN SYS_REFCURSOR
   AS
      v_cursor SYS_REFCURSOR;
   BEGIN
      OPEN v_cursor FOR
         SELECT 'hello there Sean' col1
           FROM dual
          UNION ALL
         SELECT 'here is your answer' col1
           FROM dual;      
      RETURN v_cursor;          
   END;

其中一个函数返回 VARCHAR2,另一个返回 ref 游标。在 VB 方面,你可以这样做:

Dim con As New OracleConnection("Data Source=xe;User Id=sandbox;Password=sandbox; Promotable Transaction=local")

Try
    con.Open()
    Dim cmd As OracleCommand = con.CreateCommand()
    cmd.CommandText = "test_pkg.my_func"
    cmd.CommandType = CommandType.StoredProcedure

    Dim parm As OracleParameter

    parm = New OracleParameter()
    parm.Direction = ParameterDirection.ReturnValue
    parm.OracleDbType = OracleDbType.Varchar2
    parm.Size = 5000
    cmd.Parameters.Add(parm)

    parm = New OracleParameter()
    parm.Direction = ParameterDirection.Input
    parm.Value = "abc"
    parm.OracleDbType = OracleDbType.Varchar2
    cmd.Parameters.Add(parm)

    parm = New OracleParameter()
    parm.Direction = ParameterDirection.Input
    parm.Value = 42
    parm.OracleDbType = OracleDbType.Int32
    cmd.Parameters.Add(parm)

    cmd.ExecuteNonQuery()
    Console.WriteLine("result of first function is " + cmd.Parameters(0).Value)

    '''''''''''''''''''''''''''''''''''''''''''''
    ' now for the second query
    '''''''''''''''''''''''''''''''''''''''''''''
    cmd = con.CreateCommand()
    cmd.CommandText = "test_pkg.my_func2"
    cmd.CommandType = CommandType.StoredProcedure

    parm = New OracleParameter()
    parm.Direction = ParameterDirection.ReturnValue
    parm.OracleDbType = OracleDbType.RefCursor
    cmd.Parameters.Add(parm)

    Dim dr As OracleDataReader = cmd.ExecuteReader()
    While (dr.Read())
        Console.WriteLine(dr(0))
    End While

Finally
    If (Not (con Is Nothing)) Then
        con.Close()
    End If
End Try
于 2009-11-20T22:45:11.100 回答
5
' Create ODP database connection 
Dim constr As String = (("User Id=" & Properties.Settings.[Default].DbUid & "; Password=") + Properties.Settings.[Default].DbPwd & "; Data Source=") + Properties.Settings.[Default].DbTnsName & "; Pooling=false"
Dim con As New OracleConnection(constr)
Dim cmd As New OracleCommand()
cmd.Connection = con

Try
    cmd.CommandText = "test_pkg.test_function"
    cmd.CommandType = CommandType.StoredProcedure

    'Always add return parameter before other parameters when calling database functions in .net!!
    cmd.Parameters.Add("result", OracleDbType.Int32).Direction = ParameterDirection.ReturnValue
    cmd.Parameters.Add("param1", OracleDbType.Varchar2).Value = "something"
    cmd.Parameters.Add("param2", OracleDbType.Varchar2).Value = "something else"
    con.Open()
    cmd.ExecuteNonQuery()

    Console.WriteLine(CInt(cmd.Parameters("result").Value))
Finally
    ' Cleanup
    con.Close()
    cmd.Dispose()
    con.Dispose()
End Try
于 2009-12-13T23:19:23.583 回答