Create new VB.NET project and add the following namespaces
Imports System.Data.SqlClient Imports System.IO
Create a SQL table with the following parameters:
Field Name | Field Type |
name |
nvarchar(50) |
photo |
Image |
The following code will convert Image into binary data:
Dim sql As String = "INSERT INTO Information VALUES(@name,@photo)" Dim cmd As New SqlCommand(sql, con) cmd.Parameters.AddWithValue("@name", TextBox1.Text) Dim ms As New MemoryStream() PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat) Dim data As Byte() = ms.GetBuffer() Dim p As New SqlParameter("@photo", SqlDbType.Image) p.Value = data cmd.Parameters.Add(p) cmd.ExecuteNonQuery() MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK) Label1.Visible = False TextBox1.Visible = False
To get the image from the database:
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As _ System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick cmd = New SqlCommand("select photo from Information where name='" & _ DataGridView1.CurrentRow.Cells(0).Value() & "'", con) Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte()) If Not imageData Is Nothing Then Using ms As New MemoryStream(imageData, 0, imageData.Length) ms.Write(imageData, 0, imageData.Length) PictureBox1.BackgroundImage = Image.FromStream(ms, True) End Using End If GroupBox2.SendToBack() GroupBox2.Visible = False Label1.Visible = True Label1.Text = DataGridView1.CurrentRow.Cells(0).Value() End Sub
https://www.codeproject.com/Articles/437937/Save-and-Retrieve-Image-from-a-SQL-Server-Database