Reading Blobs From Oracle

K

Ken

Hello,

I'm trying to read a Blob from Oracle and then write it to an audio
file(.AU). I'm using Visual Studio.Net 2003 (VB). I can't seem to get my
code to work. Will someone take a look at it and tell me what am I doing
wrong. Any help would be appreciated. Thanks


Dim Conn As OracleConnection = New OracleConnection("data source=XXXX;user
id=XXXXX;password=XXXXX")

Dim CMD As OracleCommand = New OracleCommand("SELECT BlobID, Audio FROM
TblBLOB", Conn)

Dim fs As FileStream

Dim bw As BinaryWriter

Dim bufferSize As integer= 100

Dim outbyte(bufferSize - 1) As Byte

Dim retval As Long

Dim startIndex As Long = 0

Dim OutPutFile As String = "



' Open the connection and read data into the DataReader.

Conn.Open()

Dim myReader As OracleDataReader =
CMD.ExecuteReader(CommandBehavior.SequentialAccess)

Do While myReader.Read()

' Get the BlobID.

OutPutFile = myReader.GetValue(0)

' Create a file to hold the output.

fs = New FileStream("OraBlob" & OutPutFile & ".AU", FileMode.OpenOrCreate,
FileAccess.Write)

bw = New BinaryWriter(fs)

' Reset the starting byte for a new BLOB.

startIndex = 0

' Read bytes into outbyte() and retain the number of bytes returned.

retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)



' Continue reading and writing while there are bytes beyond the size of the
buffer.

Do While retval = bufferSize

bw.Write(outbyte)

bw.Flush()

' Reposition the start index to the end of the last buffer and fill the
buffer.

startIndex += bufferSize

retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)

Loop

' Write the remaining buffer.

bw.Write(outbyte, 0, retval - 1)

bw.Flush()

' Close the output file.

bw.Close()

fs.Close()

Loop

' Close the reader and the connection.

myReader.Close()

Conn.Close()
 
M

Mas Jabier

Ken,

Can you specify what is the problem ? Got any error
messages ? Or the file is not created ?

If the code just running well without any error messages,
but the file is not created, you might be dealing with
security,--the ASP.NET process account does not have Write
privileges to the folder. Set it up with write privileges.

Hope this helps :)

Jody Ananda,MCAD,MCSD.Net
 
K

Ken

The file is created with 0 bytes and the code stops here

retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)

Here is the acctual message:

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 57:
Line 58: ' Read bytes into outbyte() and retain the number of
bytes returned.
Line 59: retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize)
Line 60:
Line 61:
 
M

Miha Markic

Hi Ken,

The stataments below seem ok.
You sure that this is the offending line?
Do you use Option Strict?
Is you Audio column of type BLOB?
 
K

Kenneth Adams

According to the error I receieve this is the line that it is having
problems with. I can pull images to a file but not audio files. It is
complaining about a cast error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top