Reading Blobs From Oracle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    Reading Blobs From Oracle

    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 OracleConnectio n = New OracleConnectio n("data source=XXXX;use r
    id=XXXXX;passwo rd=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(bufferS ize - 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 OracleDataReade r =
    CMD.ExecuteRead er(CommandBehav ior.SequentialA ccess)

    Do While myReader.Read()

    ' Get the BlobID.

    OutPutFile = myReader.GetVal ue(0)

    ' Create a file to hold the output.

    fs = New FileStream("Ora Blob" & OutPutFile & ".AU", FileMode.OpenOr Create,
    FileAccess.Writ e)

    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.GetByt es(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(outbyt e)

    bw.Flush()

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

    startIndex += bufferSize

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

    Loop

    ' Write the remaining buffer.

    bw.Write(outbyt e, 0, retval - 1)

    bw.Flush()

    ' Close the output file.

    bw.Close()

    fs.Close()

    Loop

    ' Close the reader and the connection.

    myReader.Close( )

    Conn.Close()





  • Mas Jabier

    #2
    Reading Blobs From Oracle

    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,MCS D.Net
    [color=blue]
    >-----Original Message-----
    >Hello,
    >
    >I'm trying to read a Blob from Oracle and then write it[/color]
    to an audio[color=blue]
    >file(.AU). I'm using Visual Studio.Net 2003 (VB). I can't[/color]
    seem to get my[color=blue]
    >code to work. Will someone take a look at it and tell me[/color]
    what am I doing[color=blue]
    >wrong. Any help would be appreciated. Thanks
    >
    >
    >Dim Conn As OracleConnectio n = New OracleConnectio n("data[/color]
    source=XXXX;use r[color=blue]
    >id=XXXXX;passw ord=XXXXX")
    >
    >Dim CMD As OracleCommand = New OracleCommand(" SELECT[/color]
    BlobID, Audio FROM[color=blue]
    >TblBLOB", Conn)
    >
    >Dim fs As FileStream
    >
    >Dim bw As BinaryWriter
    >
    >Dim bufferSize As integer= 100
    >
    >Dim outbyte(bufferS ize - 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 OracleDataReade r =
    >CMD.ExecuteRea der(CommandBeha vior.Sequential Access)
    >
    >Do While myReader.Read()
    >
    >' Get the BlobID.
    >
    >OutPutFile = myReader.GetVal ue(0)
    >
    >' Create a file to hold the output.
    >
    >fs = New FileStream("Ora Blob" & OutPutFile & ".AU",[/color]
    FileMode.OpenOr Create,[color=blue]
    >FileAccess.Wri te)
    >
    >bw = New BinaryWriter(fs )
    >
    >' Reset the starting byte for a new BLOB.
    >
    >startIndex = 0
    >
    >' Read bytes into outbyte() and retain the number of[/color]
    bytes returned.[color=blue]
    >
    >retval = myReader.GetByt es(1, startIndex, outbyte, 0,[/color]
    bufferSize)[color=blue]
    >
    >
    >
    >' Continue reading and writing while there are bytes[/color]
    beyond the size of the[color=blue]
    >buffer.
    >
    >Do While retval = bufferSize
    >
    >bw.Write(outby te)
    >
    >bw.Flush()
    >
    >' Reposition the start index to the end of the last[/color]
    buffer and fill the[color=blue]
    >buffer.
    >
    >startIndex += bufferSize
    >
    >retval = myReader.GetByt es(1, startIndex, outbyte, 0,[/color]
    bufferSize)[color=blue]
    >
    >Loop
    >
    >' Write the remaining buffer.
    >
    >bw.Write(outby te, 0, retval - 1)
    >
    >bw.Flush()
    >
    >' Close the output file.
    >
    >bw.Close()
    >
    >fs.Close()
    >
    >Loop
    >
    >' Close the reader and the connection.
    >
    >myReader.Close ()
    >
    >Conn.Close()
    >
    >
    >
    >
    >
    >.
    >[/color]

    Comment

    • Ken

      #3
      Re: Reading Blobs From Oracle

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

      retval = myReader.GetByt es(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.InvalidC astException: 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.GetByt es(1, startIndex, outbyte, 0,
      bufferSize)
      Line 60:
      Line 61:


      "Mas Jabier" <anonymous@disc ussions.microso ft.com> wrote in message
      news:048b01c3c6 c1$b6967f90$a40 1280a@phx.gbl.. .[color=blue]
      > 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,MCS D.Net
      >[color=green]
      > >-----Original Message-----
      > >Hello,
      > >
      > >I'm trying to read a Blob from Oracle and then write it[/color]
      > to an audio[color=green]
      > >file(.AU). I'm using Visual Studio.Net 2003 (VB). I can't[/color]
      > seem to get my[color=green]
      > >code to work. Will someone take a look at it and tell me[/color]
      > what am I doing[color=green]
      > >wrong. Any help would be appreciated. Thanks
      > >
      > >
      > >Dim Conn As OracleConnectio n = New OracleConnectio n("data[/color]
      > source=XXXX;use r[color=green]
      > >id=XXXXX;passw ord=XXXXX")
      > >
      > >Dim CMD As OracleCommand = New OracleCommand(" SELECT[/color]
      > BlobID, Audio FROM[color=green]
      > >TblBLOB", Conn)
      > >
      > >Dim fs As FileStream
      > >
      > >Dim bw As BinaryWriter
      > >
      > >Dim bufferSize As integer= 100
      > >
      > >Dim outbyte(bufferS ize - 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 OracleDataReade r =
      > >CMD.ExecuteRea der(CommandBeha vior.Sequential Access)
      > >
      > >Do While myReader.Read()
      > >
      > >' Get the BlobID.
      > >
      > >OutPutFile = myReader.GetVal ue(0)
      > >
      > >' Create a file to hold the output.
      > >
      > >fs = New FileStream("Ora Blob" & OutPutFile & ".AU",[/color]
      > FileMode.OpenOr Create,[color=green]
      > >FileAccess.Wri te)
      > >
      > >bw = New BinaryWriter(fs )
      > >
      > >' Reset the starting byte for a new BLOB.
      > >
      > >startIndex = 0
      > >
      > >' Read bytes into outbyte() and retain the number of[/color]
      > bytes returned.[color=green]
      > >
      > >retval = myReader.GetByt es(1, startIndex, outbyte, 0,[/color]
      > bufferSize)[color=green]
      > >
      > >
      > >
      > >' Continue reading and writing while there are bytes[/color]
      > beyond the size of the[color=green]
      > >buffer.
      > >
      > >Do While retval = bufferSize
      > >
      > >bw.Write(outby te)
      > >
      > >bw.Flush()
      > >
      > >' Reposition the start index to the end of the last[/color]
      > buffer and fill the[color=green]
      > >buffer.
      > >
      > >startIndex += bufferSize
      > >
      > >retval = myReader.GetByt es(1, startIndex, outbyte, 0,[/color]
      > bufferSize)[color=green]
      > >
      > >Loop
      > >
      > >' Write the remaining buffer.
      > >
      > >bw.Write(outby te, 0, retval - 1)
      > >
      > >bw.Flush()
      > >
      > >' Close the output file.
      > >
      > >bw.Close()
      > >
      > >fs.Close()
      > >
      > >Loop
      > >
      > >' Close the reader and the connection.
      > >
      > >myReader.Close ()
      > >
      > >Conn.Close()
      > >
      > >
      > >
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      • Miha Markic

        #4
        Re: Reading Blobs From Oracle

        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?

        --
        Miha Markic - RightHand .NET consulting & software development
        miha at rthand com


        "Ken" <KAdams@esd.wa. gov> wrote in message
        news:%23gDBig1x DHA.3140@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        > The file is created with 0 bytes and the code stops here
        >
        > retval = myReader.GetByt es(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.InvalidC astException: Specified cast is not[/color]
        valid.[color=blue]
        >
        > Source Error:
        >
        > Line 57:
        > Line 58: ' Read bytes into outbyte() and retain the number of
        > bytes returned.
        > Line 59: retval = myReader.GetByt es(1, startIndex, outbyte, 0,
        > bufferSize)
        > Line 60:
        > Line 61:
        >
        >
        > "Mas Jabier" <anonymous@disc ussions.microso ft.com> wrote in message
        > news:048b01c3c6 c1$b6967f90$a40 1280a@phx.gbl.. .[color=green]
        > > 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,MCS D.Net
        > >[color=darkred]
        > > >-----Original Message-----
        > > >Hello,
        > > >
        > > >I'm trying to read a Blob from Oracle and then write it[/color]
        > > to an audio[color=darkred]
        > > >file(.AU). I'm using Visual Studio.Net 2003 (VB). I can't[/color]
        > > seem to get my[color=darkred]
        > > >code to work. Will someone take a look at it and tell me[/color]
        > > what am I doing[color=darkred]
        > > >wrong. Any help would be appreciated. Thanks
        > > >
        > > >
        > > >Dim Conn As OracleConnectio n = New OracleConnectio n("data[/color]
        > > source=XXXX;use r[color=darkred]
        > > >id=XXXXX;passw ord=XXXXX")
        > > >
        > > >Dim CMD As OracleCommand = New OracleCommand(" SELECT[/color]
        > > BlobID, Audio FROM[color=darkred]
        > > >TblBLOB", Conn)
        > > >
        > > >Dim fs As FileStream
        > > >
        > > >Dim bw As BinaryWriter
        > > >
        > > >Dim bufferSize As integer= 100
        > > >
        > > >Dim outbyte(bufferS ize - 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 OracleDataReade r =
        > > >CMD.ExecuteRea der(CommandBeha vior.Sequential Access)
        > > >
        > > >Do While myReader.Read()
        > > >
        > > >' Get the BlobID.
        > > >
        > > >OutPutFile = myReader.GetVal ue(0)
        > > >
        > > >' Create a file to hold the output.
        > > >
        > > >fs = New FileStream("Ora Blob" & OutPutFile & ".AU",[/color]
        > > FileMode.OpenOr Create,[color=darkred]
        > > >FileAccess.Wri te)
        > > >
        > > >bw = New BinaryWriter(fs )
        > > >
        > > >' Reset the starting byte for a new BLOB.
        > > >
        > > >startIndex = 0
        > > >
        > > >' Read bytes into outbyte() and retain the number of[/color]
        > > bytes returned.[color=darkred]
        > > >
        > > >retval = myReader.GetByt es(1, startIndex, outbyte, 0,[/color]
        > > bufferSize)[color=darkred]
        > > >
        > > >
        > > >
        > > >' Continue reading and writing while there are bytes[/color]
        > > beyond the size of the[color=darkred]
        > > >buffer.
        > > >
        > > >Do While retval = bufferSize
        > > >
        > > >bw.Write(outby te)
        > > >
        > > >bw.Flush()
        > > >
        > > >' Reposition the start index to the end of the last[/color]
        > > buffer and fill the[color=darkred]
        > > >buffer.
        > > >
        > > >startIndex += bufferSize
        > > >
        > > >retval = myReader.GetByt es(1, startIndex, outbyte, 0,[/color]
        > > bufferSize)[color=darkred]
        > > >
        > > >Loop
        > > >
        > > >' Write the remaining buffer.
        > > >
        > > >bw.Write(outby te, 0, retval - 1)
        > > >
        > > >bw.Flush()
        > > >
        > > >' Close the output file.
        > > >
        > > >bw.Close()
        > > >
        > > >fs.Close()
        > > >
        > > >Loop
        > > >
        > > >' Close the reader and the connection.
        > > >
        > > >myReader.Close ()
        > > >
        > > >Conn.Close()
        > > >
        > > >
        > > >
        > > >
        > > >
        > > >.
        > > >[/color][/color]
        >
        >[/color]


        Comment

        • Kenneth Adams

          #5
          Re: Reading Blobs From Oracle

          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.


          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Kenneth Adams

            #6
            Re: Reading Blobs From Oracle



            I forgot to say that in oracle the column is a CLOB

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...