在 VB.NET 中使用 dbms_output.get_line

发布于 2024-10-31 22:21:55 字数 883 浏览 1 评论 0原文

我有一些要执行的存储过程,它们使用 dbms_output.put_line() 来将数据放入输出缓冲区。

我知道我需要使用 dbms_output.get_line(:line, :status) 来检索该输出。 我使用 System.Data.OracleClient 来避免 Oracle 部署带来的麻烦。

那么我下面的代码做错了什么?

Dim cmdSproc As OracleCommand = cnOracle.CreateCommand()
Dim strOracle As New OracleString()
Dim opaLine As New OracleParameter("lineOut", OracleType.VarChar, 255)
opaLine.Direction = ParameterDirection.Output
Dim opaStatus As New OracleParameter("status", 0)
cmdSproc.CommandText = "begin dbms_output.get_line(:lineOut,:status); end;"
cmdSproc.Parameters.Add(opaLine)
cmdSproc.Parameters.Add(opaStatus)

Dim strOutput As String = ""
strOracle = "0"
Try
   While strOracle = "0"
      cmdSproc.ExecuteOracleNonQuery(strOracle)
      strOutput = strOutput & strOracle.ToString() & vbNewLine
   End While
Catch ex As Exception
   MsgBox(ex.Message)
End Try

I have some stored procedures to execute that use dbms_output.put_line() to put data into the output buffer.

I know I need to use dbms_output.get_line(:line, :status) to retrieve that output.
I'm using System.Data.OracleClient to avoid headaches with Oracle deployment.

So what am I doing wrong with the code below?

Dim cmdSproc As OracleCommand = cnOracle.CreateCommand()
Dim strOracle As New OracleString()
Dim opaLine As New OracleParameter("lineOut", OracleType.VarChar, 255)
opaLine.Direction = ParameterDirection.Output
Dim opaStatus As New OracleParameter("status", 0)
cmdSproc.CommandText = "begin dbms_output.get_line(:lineOut,:status); end;"
cmdSproc.Parameters.Add(opaLine)
cmdSproc.Parameters.Add(opaStatus)

Dim strOutput As String = ""
strOracle = "0"
Try
   While strOracle = "0"
      cmdSproc.ExecuteOracleNonQuery(strOracle)
      strOutput = strOutput & strOracle.ToString() & vbNewLine
   End While
Catch ex As Exception
   MsgBox(ex.Message)
End Try

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

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

发布评论

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

评论(3

农村范ル 2024-11-07 22:21:55

在vb.net中SKINDER朋友的代码格式如下...感谢分享

Sub _showDbms() 
            Dim c As New OracleCommand()
            c.Connection = frmMain._cnn
            With c
                .CommandType = CommandType.Text
                .CommandText = "begin dbms_output.get_line(:line, :status); end;"
                .Parameters.Add(New OracleParameter("line", OracleDbType.Varchar2)).Size = 32000
                .Parameters("line").Direction = ParameterDirection.Output
                .Parameters.Add(New OracleParameter("status", OracleDbType.Int32))
                .Parameters("status").Direction = ParameterDirection.Output
            End With
            c.ExecuteNonQuery()
            If c.Parameters("line").Value IsNot DBNull.Value Then
                MsgBox(c.Parameters("line").Value.ToString)
            End If
    End Sub 

In vb.net the code format SKINDER friend is as follows... thnxs for sharing

Sub _showDbms() 
            Dim c As New OracleCommand()
            c.Connection = frmMain._cnn
            With c
                .CommandType = CommandType.Text
                .CommandText = "begin dbms_output.get_line(:line, :status); end;"
                .Parameters.Add(New OracleParameter("line", OracleDbType.Varchar2)).Size = 32000
                .Parameters("line").Direction = ParameterDirection.Output
                .Parameters.Add(New OracleParameter("status", OracleDbType.Int32))
                .Parameters("status").Direction = ParameterDirection.Output
            End With
            c.ExecuteNonQuery()
            If c.Parameters("line").Value IsNot DBNull.Value Then
                MsgBox(c.Parameters("line").Value.ToString)
            End If
    End Sub 
情域 2024-11-07 22:21:55

这是我如何让它工作的:(发现它此处

Dim cmdGetOutput As New OracleCommand("declare " & _
    " l_line varchar2(255); " & _
    " l_done number; " & _
    " l_buffer long; " & _
    "begin " & _
    " loop " & _
    " exit when length(l_buffer)+255 > :maxbytes OR l_done =1; " & _
    " dbms_output.get_line( l_line, l_done ); " & _
    " l_buffer := l_buffer || l_line || chr(10); " & _
    " end loop; " & _
    " :done := l_done; " & _
    " :buffer := l_buffer; " & _
    "end;", cnOracle)
cmdGetOutput.Parameters.Add("maxbytes", OracleType.Int16)
cmdGetOutput.Parameters("maxbytes").Value = 32000
cmdGetOutput.Parameters.Add("done", OracleType.Int16)
cmdGetOutput.Parameters("done").Direction = ParameterDirection.Output
cmdGetOutput.Parameters.Add("buffer", OracleType.LongVarChar, 32000)
cmdGetOutput.Parameters("buffer").Direction = ParameterDirection.Output

Dim strOutput As String = ""
Dim intStatus As Integer = 0
Try
   While True
      cmdGetOutput.ExecuteNonQuery()
      strOutput = strOutput & cmdGetOutput.Parameters("buffer").Value & vbNewLine
      If cmdGetOutput.Parameters("done").Value = 1 Then
         Exit While
      End If
   End While
Catch ex As Exception
   MsgBox(ex.Message)
Finally
   MsgBox(strOutput)
   cnOracle.Close()
End Try

Here's how I got it to work: (found it here)

Dim cmdGetOutput As New OracleCommand("declare " & _
    " l_line varchar2(255); " & _
    " l_done number; " & _
    " l_buffer long; " & _
    "begin " & _
    " loop " & _
    " exit when length(l_buffer)+255 > :maxbytes OR l_done =1; " & _
    " dbms_output.get_line( l_line, l_done ); " & _
    " l_buffer := l_buffer || l_line || chr(10); " & _
    " end loop; " & _
    " :done := l_done; " & _
    " :buffer := l_buffer; " & _
    "end;", cnOracle)
cmdGetOutput.Parameters.Add("maxbytes", OracleType.Int16)
cmdGetOutput.Parameters("maxbytes").Value = 32000
cmdGetOutput.Parameters.Add("done", OracleType.Int16)
cmdGetOutput.Parameters("done").Direction = ParameterDirection.Output
cmdGetOutput.Parameters.Add("buffer", OracleType.LongVarChar, 32000)
cmdGetOutput.Parameters("buffer").Direction = ParameterDirection.Output

Dim strOutput As String = ""
Dim intStatus As Integer = 0
Try
   While True
      cmdGetOutput.ExecuteNonQuery()
      strOutput = strOutput & cmdGetOutput.Parameters("buffer").Value & vbNewLine
      If cmdGetOutput.Parameters("done").Value = 1 Then
         Exit While
      End If
   End While
Catch ex As Exception
   MsgBox(ex.Message)
Finally
   MsgBox(strOutput)
   cnOracle.Close()
End Try
丢了幸福的猪 2024-11-07 22:21:55

在 C# 中,我使用下一个方法:

    private string GetDbmsOutputLine()
    {
        OracleCommand command = new OracleCommand
        {
            Connection = <connection>,
            CommandText = "begin dbms_output.get_line(:line, :status); end;",
            CommandType = CommandType.Text
        };

        OracleParameter lineParameter = new OracleParameter("line",  
            OracleType.VarChar);
        lineParameter.Size = 32000;
        lineParameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(lineParameter);

        OracleParameter statusParameter = new OracleParameter("status",  
            OracleType.Int32);
        statusParameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(statusParameter);

        command.ExecuteNonQuery();

        if (command.Parameters["line"].Value is DBNull)
            return null;

        string line = command.Parameters["line"].Value as string;

        return line;
    }

In C# I am using the next method:

    private string GetDbmsOutputLine()
    {
        OracleCommand command = new OracleCommand
        {
            Connection = <connection>,
            CommandText = "begin dbms_output.get_line(:line, :status); end;",
            CommandType = CommandType.Text
        };

        OracleParameter lineParameter = new OracleParameter("line",  
            OracleType.VarChar);
        lineParameter.Size = 32000;
        lineParameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(lineParameter);

        OracleParameter statusParameter = new OracleParameter("status",  
            OracleType.Int32);
        statusParameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(statusParameter);

        command.ExecuteNonQuery();

        if (command.Parameters["line"].Value is DBNull)
            return null;

        string line = command.Parameters["line"].Value as string;

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