Document writer for word RRS feed

  • Question

  • User-1659672931 posted

    I have two methods for reading BLOB field from database. One is GetBlobFieldToFileNew(which is giving corrupted documents sometimes) and second is WriteBLOBinWord(which is giving correct results). My problem is in second method I am using oracledatareader which I don't want to use. I want to correct first method and make it run. Can anybody tell me how can I correct the first method. Or can I use oracledatareader in first method Below are the two methods:

     Public Function GetBlobFieldToFileNew(ByVal username As String, ByVal password As String, ByVal database As String, ByVal caseNumber As String)
            Dim OraSession As OracleInProcServer.OraSession
            Dim OraDatabase As OracleInProcServer.OraDatabase
            Dim OraDyn As OracleInProcServer.OraDynaset
            OraSession = CreateObject("OracleInProcServer.XOraSession")
            OraDatabase = OraSession.OpenDatabase(database, username & "/" & password, 0)
            Dim strQuery As String
            strQuery = String.Format("select tempdoc_file from tempdoc where CASE_NUMBER={0}", caseNumber)
            'OraDynaset = OraDatabase.CreateDynaset(strQuery, OracleInProcServer.dynOption.ORADYN_DEFAULT)
            Dim OraFile As OracleInProcServer.OraBlob
            Dim AmountRead As Long
            Dim buffer As Object
            Dim buf() As Byte
            Dim writer As BinaryWriter
            'Using OraBlob.CopyToFile mechanism
            OraDyn = OraDatabase.CreateDynaset(strQuery, OracleInProcServer.dynOption.ORADYN_DEFAULT)
            OraFile = OraDyn.Fields("tempdoc_file").Value
            AmountRead = OraFile.Read(buffer)
            buf = buffer
            writer = New BinaryWriter(File.Open("c:\temp\blob_1.DOC", FileMode.Create, FileAccess.Write))
            'Dim fs As New FileStream("c:\temp\blob.DOC", FileMode.Create, FileAccess.Write)
            'Dim writer As New BinaryWriter(fs)
            writer.Write(buf, 0, AmountRead)
        End Function
        Sub WriteBLOBinWord(ByVal con As OracleConnection, ByVal caseNumber As String)
            Dim strFileName As String
            ' statement to get the data from the tempdoc table
            Dim cmd As OracleCommand = New OracleCommand()
            cmd.Connection = con
            cmd.CommandText = String.Format("select tempdoc_file from tempdoc where CASE_NUMBER={0}", caseNumber)
            ' use a data reader to get the data
            Dim dr As OracleDataReader = cmd.ExecuteReader()
            While dr.Read()
                strFileName = String.Format("c:\temp\blob_word_{0}_{1}.DOC", caseNumber, DateTime.Now.Millisecond)
                ' setup stream and writer objects
                Dim fs As New FileStream(strFileName, FileMode.Create, FileAccess.Write)
                Dim bw As New BinaryWriter(fs)
                Dim blob As OracleBlob = dr.GetOracleBlob(0)
                ' setup byte array with blob data from database and write to file system
                Dim bytes(blob.Length) As Byte
                blob.Read(bytes, 0, blob.Length)
                ' close the write and stream objects
            End While
            ' clean up objects
        End Sub

    Wednesday, February 12, 2014 6:45 AM