本文介绍了如何从 Visual Basic 调用带有 Ref Cursor 作为 Out 参数的 Oracle 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用从 VB 返回 SYS_REFCURSOR 的 Oracle 函数.我能够使用存储过程检索数据.我尝试以同样的方式调用 oracle 函数,但它给出了错误.

I am trying to call Oracle function which return SYS_REFCURSOR from VB. I was able to retrieve data using stored procedure. Same way I tried to call oracle function but it giving an error.

感谢有人能帮忙吗?谢谢.如果需要,我会提供屏幕截图.

Appreaciate if anyone can help ? Thanks. I'll provide screen shots if needed.

Function in DB end
====================

PKG BODY

FUNCTION GET_ALLCUST RETURN SYS_REFCURSOR AS
  getallcursor SYS_REFCURSOR;
BEGIN
    OPEN getallcursor FOR SELECT * FROM CUSTOMER;
    RETURN getallcursor;
CLOSE getallcursor;
END;

PKG Spec
==============

create or replace PACKAGE GetAllProdPckg AS
   PROCEDURE GetAllProd;
   PROCEDURE get_allprod_viasqldev;
   PROCEDURE get_allcust_viasqldev;
   FUNCTION get_allcust RETURN SYS_REFCURSOR;
   function GET_ALLPROD_FROM_DB RETURN SYS_REFCURSOR;

   END GetAllProdPckg;

** 错误 **

System.NullReferenceException: '未将对象引用设置为对象的实例.

** ERROR **

System.NullReferenceException: 'Object reference not set to an instance of an object.

我的代码 ==========================================================================

MY Code =========================================================================

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rvConn As Oracle.DataAccess.Client.OracleConnection
        rvConn = CreateConnection()
        Dim rvCmd As New Oracle.DataAccess.Client.OracleCommand
        '  Dim OracleRefCursor As New Oracle.DataAccess.Client.OracleDbType.RefCursor
        Dim ds As DataSet

        Dim oraDataAdapter As New Oracle.DataAccess.Client.OracleDataAdapter()


        Try
            rvCmd.Connection = rvConn
            rvConn.Open()

            Dim strSQL As String
            strSQL = "getallprodpckg.get_allcust"
            rvCmd.CommandType = CommandType.StoredProcedure
            rvCmd.CommandText = strSQL
            MsgBox("")

            Dim qrySerial3 As New Oracle.DataAccess.Client.OracleParameter("getallcursor", OracleDbType.RefCursor) With {
            .Direction = ParameterDirection.Output}


            rvCmd.Parameters.Add(qrySerial3)


            Dim vStr As String
            Dim reader As OracleDataReader = rvCmd.ExecuteReader

            While reader.Read()
                Console.WriteLine("{0}", reader(0) & reader(1) & reader(2))
                MsgBox(vStr)
            End While


        Catch ex As Exception
            MessageBox.Show("ERROR OCCURRED" & ex.Message)

        Finally
            rvConn.Close()
        End Try

    End Sub

** 数据库连接类**

** DB connection class **

公开课表1Private Sub TestOracleButton_Click(sender As Object, e As EventArgs) 处理 TestOracleButton.Click测试OracleConnection()结束子

Public Class Form1Private Sub TestOracleButton_Click(sender As Object, e As EventArgs) Handles TestOracleButton.ClickTestOracleConnection()End Sub

Dim custID As Integer
Dim custID2 As Integer
Dim custName As String
Dim prodID As Integer
Dim prodName As String
Dim prodPrice As Integer
Dim prodAmount As Integer
Dim custAmount As Integer
Dim custStatus As String
Dim prodQuant As Integer

Public Sub TestOracleConnection()
    Dim rvConn As Oracle.DataAccess.Client.OracleConnection
    rvConn = CreateConnection()
    Try
        rvConn.Open()
        MessageBox.Show("Oracle Connection OK")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        MessageBox.Show("No Oracle Connection established")
    Finally
        rvConn.Close()
    End Try
End Sub
Public Function CreateConnection() As Oracle.DataAccess.Client.OracleConnection
    Dim rvConn As New Oracle.DataAccess.Client.OracleConnection With {
        .ConnectionString = GetConnectionString()
    }
    Return rvConn
End Function
Public Function GetConnectionString() As String
    Dim vConnStr As String
    vConnStr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)"
    vConnStr = vConnStr & "(HOST=feenix-oracle.swin.edu.au)(PORT=1521))"
    vConnStr = vConnStr & "(CONNECT_DATA=(SERVICE_NAME=dms)));"
    vConnStr = vConnStr & "User Id=S1234564;"
    vConnStr = vConnStr & "Password=******;"
    Return vConnStr
End Function

推荐答案

你需要从参数中获取读取器,而不是从命令中获取.

You need to get the reader from the parameter, not from the command.

所以代替

Dim reader As OracleDataReader = rvCmd.ExecuteReader()

您需要:

rvCmd.CommandType = CommandType.StoredProcedure
rvCmd.CommandText = "getallprodpckg.get_allcust"

Dim qrySerial3 As New Oracle.DataAccess.Client.OracleParameter(
    None, OracleDbType.RefCursor, ParameterDirection.ReturnValue)

rvCmd.Parameters.Add(qrySerial3)

rvCmd.ExecuteNonQuery()

Dim cursor as OracleRefCursor = DirectCast(qrySerial3.Value, OracleRefCursor)

Dim reader As OracleDataReader = cursor.GetDataReader()

这篇关于如何从 Visual Basic 调用带有 Ref Cursor 作为 Out 参数的 Oracle 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:32