Solved

Insert/Read a Image to MySQL BLOB from a picturebox

Posted on 2008-06-14
6
3,692 Views
Last Modified: 2013-11-26
Hi

 I'm trying to fix the code so I can Insert a image from a picture box to a BLOB filed in my MySQL table
and also read the Image from the BLOB filed and put it back to the picturebox.

this is the code I have now, Need some help to fix it.


        Dim MySQLHandler As New SQL.MySQLDotNet.ExecuteNonQuery

        Dim FieldName As String = "Image"
        Dim QueryString As String = "UPDATE tbl_employed SET( " & FieldName & " ) = ( @" & FieldName & " ) WHERE EmpNr = '" & m_strEmployedNumber & "'"
        MySQLHandler.InsertImageToBLOB(QueryString, FieldName, PictureEditEmploy)
Public Function InsertImageToBLOB(ByVal QueryString As String, ByVal FieldName As String, ByVal PictureBox As DevExpress.XtraEditors.PictureEdit) As Integer

                Dim ms As MemoryStream = New MemoryStream       '// Create a new memory reader

                Dim bytBLOBData() As Byte                       '// Create a byte array to store picture

                Dim intBytes As Integer = 0                     '// Size of picture

                Dim iAffectedRows As Integer = 0                '// Number of affected rows

 

                Try

                    PictureBox.Image.Save(ms, ImageFormat.Jpeg) '// Get Image from picturebox

                    intBytes = CInt(ms.Length - 1)              '// Calulate byte size of image

                    ReDim bytBLOBData(intBytes)                 '// Set size if image byte array

                    ms.Position = 0                             '// Start position for reader

                    ms.Read(bytBLOBData, 0, CInt(ms.Length))    '// Read the image into the bytBLOBData variable

                    ms.Close()                                  '// Close reading stream

                Catch ex As Exception

                    Throw

                End Try

 

                '// The "Using" block will automatically dispose of the connection when we're finished

                Using MyConnectionMySQLOpen As New MySqlClient.MySqlConnection(m_strConnectionString)

 

                    Try

                        Dim prm As New MySql.Data.MySqlClient.MySqlParameter( _

                                    "@" & FieldName, _

                                    MySql.Data.MySqlClient.MySqlDbType.Blob, _

                                    bytBLOBData.Length, _

                                    ParameterDirection.Input, _

                                    False, 0, 0, Nothing, DataRowVersion.Current, bytBLOBData)

 

                        '// 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

                            .Parameters.Add(prm)

                        End With

 

                        '// Execute the SQL query with the command object, and get the affected rows in the DB back

                        iAffectedRows = cmd.ExecuteNonQuery()

 

                        '// Close the connection

                        MyConnectionMySQLOpen.Close()

 

                    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

 

                Return iAffectedRows

            End Function

 

 

          Public Function GetBLOBImage(ByVal TableName As String, ByVal FieldName As String) As Integer

                '// Create the SQL query

                Dim QueryString As String = "SELECT " & FieldName & " FROM " & TableName

                Dim iAffectedRows As Integer = 0                '// Number of affected rows

                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()

 

                        FileSize = myData.GetUInt32(myData.GetOrdinal("file_size"))

                        rawData = New Byte(CInt(FileSize)) {}

 

                        myData.GetBytes(myData.GetOrdinal(FieldName), 0, rawData, 0, CInt(FileSize))

 

                        fs = New FileStream("C:\newfile.png", FileMode.OpenOrCreate, FileAccess.Write)

                        fs.Write(rawData, 0, CInt(FileSize))

                        fs.Close()

 

                    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

 

                Return iAffectedRows

            End Function

Open in new window

0
Comment
Question by:AWestEng
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:team2005
ID: 21788015
Hi!

Is this what you locking for
http://dev.mysql.com/doc/refman/5.0/en/blob.html

0
 
LVL 1

Author Comment

by:AWestEng
ID: 21789036
that's in C and not using the .Net connector so what I need help with is to "clean up" the code I have in the snippet above. thx anyway.
0
 
LVL 2

Expert Comment

by:team2005
ID: 21791363
Hi!

Do you get any error message, or what is not working ?

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 1

Author Comment

by:AWestEng
ID: 21791460
The code above is more like "pseudo" code, so no it dosen't work yet. But the code is almost there. It should only need some smal fixing. But that's what I need help with.
0
 
LVL 1

Author Comment

by:AWestEng
ID: 21791464
the
PictureBox As DevExpress.XtraEditors.PictureEdit can be changed to
PictureBox As PictureBox
0
 
LVL 1

Accepted Solution

by:
AWestEng earned 0 total points
ID: 21826056
the question seems dead. the solution is just to change the "@" to a "?"

I created a new question here, this one is for the Update
http://www.experts-exchange.com/Database/MySQL/Q_23499277.html
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now