Save and Retrieve an Image in a Database Using VB.Net

0
349

This tutorial will help you how to Save and Retrieve an Image in a Database Using VB.Net. In this article, we will be using Visual Basic 2008 and MS Access 2007 for our database.

Please follow the instruction below:

  • Step 1: Create a database in MS Access
    • Database Name:  studentdb.accdb
    • Table: tblstudent
      • The structure of tblstudent:

 

Fields DataType
studID Autonumber
FNAME Text
LNAME Text
COURSE Text
YR Text
AGE Text
EAMIL Text
studimg OLE Object
  • Step 2: Create a Visual Basic 2008 Project
    • Create and Save it as “Saveandrettriveimg”
    •  Then Put the database(studentdb.accdb) inside Project execution folder(\bin\debug)
    • Design the form like as shown below.
Save and Retrieve Image from Database
Save and Retrieve Image from Database
  • Step 3: Writing code in Visual Basic
    • Under Public Class Add the Following Code:
  1. Dim conn As New OleDb.OleDbConnection
  2. Dim Myconnection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\studentdb.accdb"
  3. Dim cmd As New OleDb.OleDbCommand
  4. Dim da As New OleDb.OleDbDataAdapter
  5. Dim result As Integer
  6. Dim imgpath As String
  7. Dim arrImage() As Byte
  8. Dim sql As String
    • On Click “Browse Image” Button.

  1. Try
  2.  
  3. Dim OFD As FileDialog = New OpenFileDialog()
  4.  
  5. OFD.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"
  6.  
  7. If OFD.ShowDialog() = DialogResult.OK Then
  8. imgpath = OFD.FileName
  9. Pic1.ImageLocation = imgpath
  10.  
  11. End If
  12.  
  13. OFD = Nothing
  14.  
  15. Catch ex As Exception
  16. MsgBox(ex.Message.ToString())
  17. End Try
    • On Click “SAVE” Button.
  1. Try
  2.  
  3. Dim mstream As New System.IO.MemoryStream()
  4.  
  5. Pic1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
  6. arrImage = mstream.GetBuffer()
  7. Dim FileSize As UInt32
  8. FileSize = mstream.Length
  9. mstream.Close()
  10.  
  11. conn.ConnectionString = Myconnection
  12. conn.Open()
  13. sql = "INSERT INTO tblstudent(FNAME,LNAME, COURSE, YR, AGE, EMAIL,studimg) " & _
  14. " VALUES (@a, @LNAME, @COURSE, @YR, @AGE, @EMAIL,@studimg)"
  15.  
  16. cmd.Connection = conn
  17. cmd.CommandText = sql
  18. cmd.Parameters.AddWithValue("@a", TXTFIRSTNAME.Text)
  19. cmd.Parameters.AddWithValue("@LNAME", TXTLNAME.Text)
  20. cmd.Parameters.AddWithValue("@COURSE", TXTCOURSE.Text)
  21. cmd.Parameters.AddWithValue("@YR", TXTYR.Text)
  22. cmd.Parameters.AddWithValue("@AGE", TXTAGE.Text)
  23. cmd.Parameters.AddWithValue("@EMAIL", TXTEMAIL.Text)
  24. cmd.Parameters.AddWithValue("@studimg", arrImage)
  25. Dim r As Integer
  26. r = cmd.ExecuteNonQuery()
  27. If r > 0 Then
  28. MsgBox("Student Record hass been Saved!")
  29. Else
  30. MsgBox("No record has been saved!")
  31. End If
  32. conn.Close()
  33.  
  34. Catch ex As Exception
  35. MessageBox.Show(ex.Message)
  36. End Try
    • On Click “Search” Button
  1. sql = "Select * from tblstudent where studID=" & Val(TextBox1.Text)
  2. conn.ConnectionString = Myconnection
  3. conn.Open()
  4. With cmd
  5. .Connection = conn
  6. .CommandText = sql
  7. End With
  8. Dim arrImage() As Byte
  9. Dim publictable As New DataTable
  10. Try
  11. da.SelectCommand = cmd
  12. da.Fill(publictable)
  13. TXTLNAME.Text = publictable.Rows(0).Item(2)
  14. TXTFIRSTNAME.Text = publictable.Rows(0).Item(1)
  15. TXTCOURSE.Text = publictable.Rows(0).Item(3)
  16. TXTYR.Text = publictable.Rows(0).Item(4)
  17. TXTAGE.Text = publictable.Rows(0).Item(5)
  18. TXTEMAIL.Text = publictable.Rows(0).Item(6)
  19. arrImage = publictable.Rows(0).Item(7)
  20.  
  21. Dim mstream As New System.IO.MemoryStream(arrImage)
  22. Pic1.Image = Image.FromStream(mstream)
  23.  
  24. Catch ex As Exception
  25. MsgBox(ex.Message)
  26. Finally
  27. da.Dispose()
  28. conn.Close()
  29.  
  30. End Try

 

  • Step 4: Run your Visual Basic Project
    • Press “F5” or Click the Play Button.
You can Download the full Source Code saveandretriveimage

 

If you find this tutorial helpful please share this to your friends and to help us grow please hit the LIKE button in our Facebook Page. Thank You.

 

Facebook Comments
(Visited 554 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here