VB.NET Update BLOB in MySQL
The code below that reads an image from the pic box into a memorystream
and inserts it into a MySQL BLOB in the database, works perfectly. Can
retrieve from the database and display in the pic box works perfectly (not
shown). Just a side note, I didn't write that code, it is from a tutorial
on the web.
The bit I wrote to UPDATE the database does not work. I have tried many
combinations of the brackets, single quotes, double quotes but no luck
yet. I get varies error messages from the CATCH, sometimes in plain
English referring to syntax and sometimes in a binary dump. When I do get
a successful update message, all that is written into the BLOB is the name
of what I am trying to update "VALUES(@image_data)". I have tried to
UPDATE from both a memorystream and a file but no luck yet.
The "rem'd" code works perfectly for INSERT and I can manually update the
BLOB but not practical. The BLOB would be updated for example when I
upgrade a SD-DVD to BD-DVD, I will change the small logo.
Before I get flamed, I know not good practice to store the images but in
this case, it is more practical I believe. The images are tiny 24x11 DVD
logo's that are read into a datagridview table, all my other images are
stored as files on the server (1000's of them) however, the test example
is just reading a cover image.
Can anyone help me correct the code or suggest a better method? Thanks….
Private Sub btnUpdate_Click(sender As System.Object, e As
System.EventArgs) Handles btnUpdate.Click
Dim FileSize As UInt32
'temp for testing
Dim carjackedfront As String = "8f17cd4a-8dd6-4ec1-9e7b-7f4d50460693"
'get picture from database
Dim nvcCover As String = carjackedfront
'Dim original As Image = Image.FromFile("D:\Pics\ae.jpg")
Dim original As Image = Image.FromFile(mediastorageCovers & nvcCover &
pictureformat)
Dim mstream As New System.IO.MemoryStream()
' -----this line saves image from picture box
'pic_box_save.Image.Save(mstream,
System.Drawing.Imaging.ImageFormat.Jpeg)
' -----This line saves image from file into memory stream
original.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
FileSize = mstream.Length
pic_box_get.Image = Image.FromStream(mstream)
mstream.Close()
MsgBox("File Size = " & FileSize)
Try
sql = "UPDATE image_in_db SET Test = VALUES(@image_Text) WHERE
id = '1'"
'sql = "INSERT INTO image_in_db(id, image_data) VALUES(@image_id,
@image_data)"
sql_command = New MySqlClient.MySqlCommand(sql, sql_connection)
' sql_command.Parameters.AddWithValue("@image_id", Nothing)
sql_command.Parameters.AddWithValue("@image_data", arrImage)
sql_command.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
MsgBox("Image has been UPDATED.")
End Sub
No comments:
Post a Comment