Link to home
Start Free TrialLog in
Avatar of AWestEng
AWestEngFlag for Sweden

asked on

Image BLOB, Read, MySQL

Hi

I'm trying to fix my code so I can read a image from  BLOB filed in my MySQL table and then put it in my picturebox.

Here is the code

Public Sub GetBLOBImage(ByVal TableName As String, ByVal FieldName As String, ByVal PictureBox As PictureBox)
    '// Create the SQL query
    Dim QueryString As String = "SELECT Image FROM tbl_employed WHERE EmpNr = 1234"""
    Dim rawData() As Byte
    Dim FileSize As UInt32
    Dim fs As FileStream
 
    '// The "Using" block will automatically dispose of the connection when we're finished
    Using MyConnectionMySQLOpen As New MySqlClient.MySqlConnection(m_strConnectionString)
 
        Try
            '// Open the DB connection
            MyConnectionMySQLOpen.Open()
 
            '// Create a new command object
            Dim cmd As New MySqlClient.MySqlCommand()
 
            '// Set command properties
            With cmd
                .Connection = MyConnectionMySQLOpen
                .CommandType = CommandType.Text
                .CommandText = QueryString
            End With
 
            '// Execute the SQL query with the command object, and get the affected rows in the DB back
            Dim myData As MySqlDataReader = cmd.ExecuteReader
 
            '// Close the connection
            MyConnectionMySQLOpen.Close()
 
            If Not myData.HasRows Then Throw New Exception("There are no BLOBs data")
 
            myData.Read()
 
            '////////////////// HERE is where I need the help to complete the code//////////////////////////
            FileSize = myData.xxxxxx
            rawData = New Byte(CInt(FileSize)) {}
 
            myData.GetBytes(myData.GetOrdinal(FieldName), 0, rawData, 0, CInt(FileSize))
 
            PictureBox.Image = xxxxxxxxxxxxxx
 
        Catch MyException As MySqlException
            Throw
        Catch ex As Exception
            Throw
        Finally
            '// Close connection if an exception was thrown before the connection could close
            If MyConnectionMySQLOpen.State = ConnectionState.Open Then
                MyConnectionMySQLOpen.Close()
            End If
        End Try
    End Using
 
End Sub

Open in new window

Avatar of mankowitz
mankowitz
Flag of United States of America image

There is a forum article that gives some sample code.
Check out http://forums.mysql.com/read.php?38,6172,186617#msg-186617

' This assumes that the filesize is stored as a separate column in the table
FileSize = myData.GetUInt32(myData.GetOrdinal("filesize"))
rawData = New Byte(FileSize) {}
'get the bytes and filesize
myData.GetBytes(myData.GetOrdinal("file"), 0, rawData, 0, FileSize)
Dim ad As New MemoryStream(100000)
Dim bm As Bitmap
ad.Write(rawData, 0, FileSize)
bm = New Bitmap(ad)
Avatar of AWestEng

ASKER

oki. but must I have the filesize in the table?

is it not possible to solve it otherwise?
ASKER CERTIFIED SOLUTION
Avatar of mankowitz
mankowitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oki, thx I will test it and get back to you