Solved

Image BLOB, MySQL

Posted on 2008-06-19
7
987 Views
Last Modified: 2013-11-26
Hi

I'm trying to fix my code so I can Insert a image from a picture box to a BLOB filed in my MySQL table

I'm not sure how to create the the Query.
Dim QueryString As String = "UPDATE tbl_employed SET(Image) = (@Image) WHERE EmpNr = 1234"
Public Function InsertImageToBLOB(ByVal QueryString As String, ByVal FieldName As String, ByVal PictureBox As PictureBox) 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

Open in new window

0
Comment
Question by:AWestEng
  • 6
7 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 21823723
code looks good, but mysql parameters are marked with ? instead of @.

i.e. SET(Image) = (?Image)
0
 
LVL 1

Author Comment

by:AWestEng
ID: 21825658
same in this code then?

  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)
 

0
 
LVL 1

Author Comment

by:AWestEng
ID: 21825675
hmm are you sure about the ? the .Net connector generates @ if I use a DataAdapter in a dataset
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:AWestEng
ID: 21825734
oki. you might be right there :)

No I got this exception
"Data too long for column 'Image' at row 1"
0
 
LVL 1

Author Comment

by:AWestEng
ID: 21825888
thx m8.. I change to LONGBLOB the Imgae size was 250 kB
0
 
LVL 1

Author Comment

by:AWestEng
ID: 21825898
mankowitz:are you still there?

I will give you the points, but I need help with a reading the blob from MySql an put it back to the picturebox.

I will create a new question, can you help me with that?
0
 
LVL 1

Author Comment

by:AWestEng
ID: 21826174
here is where the new question is created

http://www.experts-exchange.com/Database/MySQL/Q_23500417.html
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Creating and Managing Databases with phpMyAdmin in cPanel.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

911 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

20 Experts available now in Live!

Get 1:1 Help Now