【问题标题】:Replacing Oracle newline characters in a .NET string替换 .NET 字符串中的 Oracle 换行符
【发布时间】:2011-04-27 23:32:35
【问题描述】:

我正在使用 Oracle 命令 dbms_output.get_line 从存储过程中检索输出。一旦我的代码运行命令,我将通过一个 32000 字节长的缓冲区字符串检索结果。

这个字符串不可避免地会有 Oracle 换行符 (chr(10) 和 chr(13)) 我想用 Environment.NewLine 替换所以我可以在标准的 Winforms 文本框中显示输出。

这是我一直在使用的代码 - 我不记得我现在确切地从哪里获得了 Oracle 命令,但如果我找到它,我会添加链接。

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 List(Of String) = New List(Of String)
Dim intStatus As Integer = 0
Try
   While True
      cmdGetOutput.ExecuteNonQuery()
      strOutput.Add(cmdGetOutput.Parameters("buffer").Value)
      If cmdGetOutput.Parameters("done").Value = 1 Then
         Exit While
      End If
   End While
Catch ex As Exception
   MsgBox(ex.Message)
Finally
   For ixLines as Integer = 0 to strOutput.Count - 1
      txtOutput.Text = txtOutput.Text & strOutput(ixLines)
   Next
End Try

当我运行这个时,txtOutput 将在应该换行的地方有方框字符。

谢谢大家的帮助!

【问题讨论】:

    标签: vb.net winforms oracle oracle9i


    【解决方案1】:

    首先,该代码甚至不应该编译。您不能将 List(of String) 分配给 TextBox.Text 属性。

    其次,String类定义了一个Replace(searchstring, replacementstring)方法。

    【讨论】:

    • 我以为 Oracle 中的 Chr(10) 和 Chr(13) 与 .NET 中的不同,但我错了。
    最近更新 更多