Reading Blobs From Oracle

  • Thread starter Thread starter Ken
  • Start date Start date
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()
 
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
 
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:
 
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?
 
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.
 
Back
Top