How to Save an Image in Access Database Using VB.Net

Today, you will learn how to save Image in Access Database using VB.Net. This project will illustrate how to save an object in the field with a data type of “OLE Object” in the Access database. In this process, the image itself will be automatically saved in the database when the button is clicked. See the procedure below 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. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB to access OLeDB libraries and .
  1. 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.
  1.  
  2. Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dbsaveimg.accdb")
  3. Dim cmd As OleDbCommand
  4. Dim sql As String

Step 5

Create a method for saving an image in the database.
  1.  
  2. Private Sub saveImage(sql As String)
  3. Try
  4.  
  5. Dim arrImage() As Byte
  6. Dim mstream As New System.IO.MemoryStream()
  7.  
  8. 'SPECIFIES THE FILE FORMAT OF THE IMAGE
  9. PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
  10.  
  11. 'RETURNS THE ARRAY OF UNSIGNED BYTES FROM WHICH THIS STREAM WAS CREATED
  12. arrImage = mstream.GetBuffer()
  13.  
  14. 'GET THE SIZE OF THE STREAM IN BYTES
  15. Dim FileSize As UInt32
  16. FileSize = mstream.Length
  17. 'CLOSES THE CURRENT STREAM AND RELEASE ANY RESOURCES ASSOCIATED WITH THE CURRENT STREAM
  18. mstream.Close()
  19.  
  20. con.Open()
  21.  
  22. cmd = New OleDbCommand
  23. With cmd
  24. .Connection = con
  25. .CommandText = sql
  26. .Parameters.AddWithValue("@img", arrImage)
  27. .ExecuteNonQuery()
  28. End With
  29. Catch ex As Exception
  30. MsgBox(ex.Message)
  31. Finally
  32. con.Close()
  33. End Try
  34. End Sub

Step 6

Write the following code for getting the image from the external directory.
  1.  
  2. Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
  3. Try
  4. With OpenFileDialog1
  5.  
  6. 'CHECK THE SELECTED FILE IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  7. .CheckFileExists = True
  8.  
  9. 'CHECK THE SELECTED PATH IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  10. .CheckPathExists = True
  11.  
  12. 'GET AND SET THE DEFAULT EXTENSION
  13. .DefaultExt = "jpg"
  14.  
  15. 'RETURN THE FILE LINKED TO THE LNK FILE
  16. .DereferenceLinks = True
  17.  
  18. 'SET THE FILE NAME TO EMPTY
  19. .FileName = ""
  20.  
  21. 'FILTERING THE FILES
  22. .Filter = "(*.jpg)|*.jpg|(*.png)|*.png|(*.jpg)|*.jpg|All files|*.*"
  23. 'SET THIS FOR ONE FILE SELECTION ONLY.
  24. .Multiselect = False
  25.  
  26. 'SET THIS TO PUT THE CURRENT FOLDER BACK TO WHERE IT HAS STARTED.
  27. .RestoreDirectory = True
  28.  
  29. 'SET THE TITLE OF THE DIALOG BOX.
  30. .Title = "Select a file to open"
  31.  
  32. 'ACCEPT ONLY THE VALID WIN32 FILE NAMES.
  33. .ValidateNames = True
  34.  
  35. If .ShowDialog = DialogResult.OK Then
  36. Try
  37. PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
  38. Catch fileException As Exception
  39. Throw fileException
  40. End Try
  41. End If
  42.  
  43. End With
  44. Catch ex As Exception
  45. MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
  46. End Try
  47. End Sub

Step 7

Do the following code for saving the picture when the button is clicked.
  1.  
  2. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3. sql = "Insert into tblimage (img) Values (@img)"
  4. saveImage(sql)
  5. MsgBox("Image has been saved in the database")
  6. End Sub
Output: ps4 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

Comments

Submitted bylelouch123 (not verified)on Wed, 02/26/2020 - 21:39

hello, thank you so much for the code. It helps a lot. Now I have a big problem, this is how can I retrieve the image from access with ole object data type by using datagridview cell content click? PLEASE I REALLY NEED HELP WITH THIS ONE. I'm making a program where if the user clicks any cell it will retrieve the data and put it to respective controls.
Submitted byAnonymous (not verified)on Wed, 09/23/2020 - 01:10

Good sirji

Add new comment