dbTalk Databases Forums  

solution: returning the contents of dbms_output after running a sp in vb .net

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss solution: returning the contents of dbms_output after running a sp in vb .net in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Kevin
 
Posts: n/a

Default solution: returning the contents of dbms_output after running a sp in vb .net - 10-23-2006 , 05:53 AM






righty, after a bit of investigation I found a solution.
I have take tom kytes code from here:
http://asktom.oracle.com/pls/ask/f?p...D:139812348065

and converted it to vb .net using the corelab Oracle driver. Probably
most people will want to change it to the oledb or oracle driver. I
have tried to keep it as true to the original as possible.

Code:

Imports CoreLab.Oracle
Public Class DBMSOutput
'
' our instance variables. It is always best to
' use callable or prepared statements and prepare (parse)
' them once per program execution, rather then one per
' execution in the program. The cost of reparsing is
' very high. Also -- make sure to use BIND VARIABLES!
'
' we use three statments in this class. One to enable
' dbms_output - equivalent to SET SERVEROUTPUT on in SQL*PLUS.
' another to disable it -- like SET SERVEROUTPUT OFF.
' the last is to "dump" or display the results from dbms_output
' using system.out
'
'
Private cmdEnable As OracleCommand
Private cmdDisable As OracleCommand
Private cmdShow As OracleCommand

'
' our constructor simply prepares the three
' statements we plan on executing.
'
' the statement we prepare for SHOW is a block of
' code to return a String of dbms_output output. Normally,
' you might bind to a PLSQL table type but the jdbc drivers
' don't support PLSQL table types -- hence we get the output
' and concatenate it into a string. We will retrieve at least
' one line of output -- so we may exceed your MAXBYTES parameter
' below. If you set MAXBYTES to 10 and the first line is 100
' bytes long, you will get the 100 bytes. MAXBYTES will stop us
' from getting yet another line but it will not chunk up a line.
'
'
Public Sub New(ByVal conn As OracleConnection)
cmdEnable = New OracleCommand("begin dbms_output.enable(:1);
end;", conn)
cmdEnable.Parameters.Add("1", OracleDbType.Number)

cmdDisable = New OracleCommand("begin dbms_output.disable;
end;", conn)

cmdShow = 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;", conn)
cmdShow.Parameters.Add("maxbytes", OracleDbType.Integer).Value
= 32000
cmdShow.Parameters.Add("done", OracleDbType.Integer).Direction
= ParameterDirection.Output
cmdShow.Parameters.Add("buffer",
OracleDbType.VarChar).Direction = ParameterDirection.Output
End Sub

'
' enable simply sets your size and executes
' the dbms_output.enable call
'
'
Public Sub Enable(ByVal size As Integer)
cmdEnable.Parameters("1").Value = size
cmdEnable.ExecuteNonQuery()
End Sub


'
' disable only has to execute the dbms_output.disable call
'
Public Sub Disable()
cmdDisable.ExecuteNonQuery()
End Sub

'
' show does most of the work. It loops over
' all of the dbms_output data, fetching it in this
' case 32,000 bytes at a time (give or take 255 bytes).
' It will print this output on stdout by default (just
' reset what System.out is to change or redirect this
' output).
'
Public Sub Show()
Dim done As Integer = 0

While True
cmdShow.ExecuteNonQuery()
Console.WriteLine(cmdShow.Parameters("buffer").Val ue)
If cmdShow.Parameters("done").Value = 1 Then Exit While
End While

End Sub

End Class



it might be better to return the output as a string but I'm sure people
can modify it as required. For its usage, check out the tom kyte post.


Reply With Quote
  #2  
Old   
Kevin
 
Posts: n/a

Default Re: solution: returning the contents of dbms_output after running a sp in vb .net - 10-31-2006 , 11:44 AM






ok, i found a mistake:
cmdShow.Parameters.Add("buffer", OracleDbType.VarChar).Direction =
ParameterDirection.Output

should become
cmdShow.Parameters.Add("buffer", OracleDbType.Long).Direction =
ParameterDirection.Output

I didnt realise the oracle data type of Long is a big string as opposed
to the vb net type wihich is a big integer, and I was getting overflows


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.