VB.NET View Image from Database and Save Image Binary/Photo to Database Programmatically

vb-net-view-image-from-database-and-save-image-binary-photo-to-database-programmaticallyVB.NET View Image from Database and Save Image Binary/Photo to Database Programmatically.

View Image Binary/Photo from Database Programmatically :

Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & IO.Path.GetDirectoryName(Application.ExecutablePath) & "\db.accdb")
cn.Open()
Dim da As New OleDbDataAdapter("SELECT * FROM employee", cn)
Dim tb As New DataTable()
da.Fill(tb)
DataGridView1.DataSource = tb

picPhoto.image=Nothing
Try
	Dim imagedata As Object = DataGridView1.Rows(e.RowIndex).Cells("photo").Value
	Dim ms As New MemoryStream(imagedata, 0, imagedata.length)
	ms.Write(imagedata, 0, imagedata.length)
	picPhoto.Image = Image.FromStream(ms)
Catch ex As Exception

End Try

Save Image Binary/Photo to Database Programmatically :

        Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & IO.Path.GetDirectoryName(Application.ExecutablePath) & "\db.accdb")
        cn.Open()
        Dim cm As New OleDbCommand("INSERT INTO employee (id,name,photo) VALUES ('" & txtid.Text & "','" & txtname.Text & "',?)", cn)
        cm.Parameters.Add("@photo", OleDbType.VarBinary).Value = File.ReadAllBytes(txtFilePhoto.Text)
        cm.ExecuteNonQuery()

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>