This time, I will teach you
how to Update an Image in Access Database Using VB.net. I came up with this idea because this is a
very important function when you are dealing with the database object. This method has the ability to
update the stored image in the access database. It is also has a
function that can view an image in the large view. It is a simple procedure that you can do even if you are a
beginner in programming. Just follow the step by step guide to see how it works.
Creating an Application
Step 1
Open
Microsoft Visual Studio 2015 and create a new windows form application in visual basic.
Step 2
Do the form just like shown below.
Step 3
Press F7 to open the code editor. In the code editor, add a namespace for
OLeDB
to access
OLeDB
libraries.
Imports System.Data.OleDb
Step 4
Create a
connection between the access database and c#. After that, declare all the
classes and variables that are needed.
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Application.StartupPath & "\dbsaveimg.accdb")
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Dim dt As DataTable
Dim sql As String
Step 5
Create a method for
retrieving images in the database.
Private Sub loadData(sql As String, dtg As DataGridView)
Try
con.Open()
cmd = New OleDbCommand
da = New OleDbDataAdapter
dt = New DataTable
With cmd
.Connection = con
.CommandText = sql
End With
da.SelectCommand = cmd
da.Fill(dt)
dtg.DataSource = dt
dtg.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
dtg.Columns(0).Visible = False
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
da.Dispose()
End Try
End Sub
Step 6
Create a method for
updating an image in the database.
Private Sub executeQuery(sql As String)
Try
Dim arrImage() As Byte
Dim mstream As New System.IO.MemoryStream()
'SPECIFIES THE FILE FORMAT OF THE IMAGE
PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
'RETURNS THE ARRAY OF UNSIGNED BYTES FROM WHICH THIS STREAM WAS CREATED
arrImage = mstream.GetBuffer()
'GET THE SIZE OF THE STREAM IN BYTES
Dim FileSize As UInt32
FileSize = mstream.Length
'CLOSES THE CURRENT STREAM AND RELEASE ANY RESOURCES ASSOCIATED WITH THE CURRENT STREAM
mstream.Close()
con.Open()
cmd = New OleDbCommand
With cmd
.Connection = con
.CommandText = sql
.Parameters.AddWithValue("@img", arrImage)
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
da.Dispose()
End Try
End Sub
Step 7
Write the following code for
resizing an image in the first load of the form.
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return
If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType = GetType(Byte()) Then
CType(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex),
DataGridViewImageCell).ImageLayout = DataGridViewImageCellLayout.Zoom
End If
End Sub
Step 8
Write the following code for
retrieving an image in the first load of the form
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
sql = "Select ID,img as Picture From tblimage"
loadData(sql, DataGridView1)
End Sub
Step 9
Write the following code for
passing the image from the datagridview into the picture box.
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim arrImage() As Byte
arrImage = DataGridView1.CurrentRow.Cells(1).Value
Dim mstream As New System.IO.MemoryStream(arrImage)
PictureBox1.Image = Image.FromStream(mstream)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Step 10
Double click the “
Browse” button to open the
click event handler
of it and do the following code to
get an image in the explore and place it in the picture box.
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
Step 11
Double click again the “
Update” button to open the
click event handler
of it and do the following codes to
update the image in the database.
sql = "UPDATE tblimage SET img = @img WHERE ID=" & DataGridView1.CurrentRow.Cells(0).Value
executeQuery(sql)
sql = "Select ID,img as Picture From tblimage"
loadData(sql, DataGridView1)
Note : The database file is in the bin folder of the project.
Download the complete source code and run it on your computer.
For any questions about this article. You can contact me @
Email –
[email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below