Thread: MySql Blobs

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    MySql Blobs


    Hi there

    Im trying to Insert a Microsoft Word Doc in to my MySql Database

    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 trying to use. it appears to upload fine but when i try to download it, it come back corrupt.

    can anyone see where im going wrong. Is what om trying to do even possible.

    I have managed to do this successfully in the workbench application but never in VB

    Ian
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,439
    Rep Power
    4539
    I think there is an article here at devshed about storing blob's in mysql. I've never done it in VB either, but in general I believe you need to get the binary data into the db and also store the mime type, and then set the mime type when you retrieve the blob.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester

IMN logo majestic logo threadwatch logo seochat tools logo