Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42292

VS 2010 insert file in to MYsql

$
0
0
Hi There

Im trying to upload a word doc in to my Mysql Database and im having some trouble. I can upload fine (it seems) but when ever i then try to download it, ti come back corrupt



this is the code that using to do the upload


Code:

Dim cmd As New MySqlCommand
        Dim SQL As String
        Dim FileSize As UInt32
        Dim rawData() As Byte
        Dim fs As FileStream
        Try
            fs = New FileStream("c:\cright\MJTinvoice.doc", FileMode.Open, FileAccess.Read)
            FileSize = fs.Length

            rawData = New Byte(FileSize) {}
            fs.Read(rawData, 0, FileSize)
            fs.Close()

            Conn.Open()

            SQL = "INSERT INTO Documents (Document) VALUES(?FileName)"

            cmd.Connection = Conn
            cmd.CommandText = SQL
            cmd.Parameters.AddWithValue("?FileName", rawData)
           

            cmd.ExecuteNonQuery()

            MessageBox.Show("File Inserted into database successfully!", _
            "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("There was an error: " & ex.Message, "Error", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

this is the code that im using for the download

Code:

Dim Fs As FileStream
        Dim bw As BinaryWriter
        Dim retval As Long
        Dim startIndex As Long = 0
        Dim bs As Integer = 100 'buffer size
        Dim outbyte(bs - 1) As Byte
        Dim Cmd As New MySqlCommand("SELECT Document FROM Documents WHERE DocumentId = 1", Conn)
        Conn.Open()
        Dim dr As MySqlDataReader = Cmd.ExecuteReader
        Do While dr.Read
            Dim strtmp As String = dr.GetString(0)
            Dim fn As String = "C:\cright\test.doc"
            Fs = New FileStream(fn, FileMode.OpenOrCreate, FileAccess.Write)
            bw = New BinaryWriter(Fs)
            startIndex = 0
            retval = dr.GetBytes(0, startIndex, outbyte, 0, bs)
            Do While retval = bs
                bw.Write(outbyte)
                bw.Flush()
                startIndex += bs
                retval = dr.GetBytes(0, startIndex, outbyte, 0, bs)
            Loop
            Try
                bw.Write(outbyte, 0, retval - 1)
            Catch
            End Try
            bw.Flush()
            ' Close the output file.
            bw.Close()
            Fs.Close()
        Loop
        dr.Close()
        Conn.Close()

any help much appreciated :)

Ian

Viewing all articles
Browse latest Browse all 42292

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>