This tutorial is the continuation of my last tutorial which is
How to Save Image In MySQL Database Using VB.Net. This time I’m going to teach you how to
Retrieve Image in MySQL Database Using VB.Net. In this method, it will
retrieve the image or file from the MySQL database into the picturebox..
Lets begin:
Open the
Project that you have created in the previous tutorial then add the button in the form just like shown below.

Add another Form in the project and do the form just like this.

double click the second Form to fire the code view. In the code view add
Imports MySql.Data.MySqlClient
and
Imports System.IO
above the
Public Class for you imports.
Go back to the design view, click the
Retrieve button inside the second form to open the click event handler of it. After that do the following code for
retrieving data from MySQL database into the datagridview.
Try
con.Open()
cmd = New MySqlCommand
With cmd
.Connection = con
.CommandText = "SELECT ImageID,Caption FROM tblimageblob"
End With
da = New MySqlDataAdapter
da.SelectCommand = cmd
dt = New DataTable
da.Fill(dt)
With dtglist
.DataSource = dt
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
da.Dispose()
con.Close()
End Try
Go back to the design view again and double click the View Image button open the click event handler of it. Do the following code for
retrieving image in the picture box.
Try
con.Open()
cmd = New MySqlCommand
With cmd
.Connection = con
.CommandText = "SELECT Caption,ImageFile FROM tblimageblob WHERE ImageID=" & Val(dtglist.CurrentRow.Cells(0).FormattedValue)
End With
da = New MySqlDataAdapter
dt = New DataTable
Dim arrImage() As Byte
da.SelectCommand = cmd
da.Fill(dt)
txtCaption.Text = dt.Rows(0).Item(0)
arrImage = dt.Rows(0).Item(1)
Dim mstream As New System.IO.MemoryStream(arrImage)
PictureBox1.Image = Image.FromStream(mstream)
Catch ex As Exception
MsgBox(ex.Message)
Finally
da.Dispose()
con.Close()
End Try
Output