VB.Net Insert Image Into Sql Database

VB.NET - How To Save Image Into SQL Server DataBase Using Visual Basic .Net

                                                                                                                         

In This VB.NET Tutorial We Will See How To Insert / Save Image In DataBase Using Microsoft SQL DataBase And Visual Basic .NET Programming Language.

Part 1

Part 2

Project Source Code:

Imports System.Data.SqlClient
Imports System.IO

Public Class VBNET_Insert_Image_Into_SQL

    Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB; Integrated Security = true")
    Private Sub BTN_BROWSE_Click(sender As Object, e As EventArgs) Handles BTN_BROWSE.Click

        Dim opf As New OpenFileDialog

        opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"

        If opf.ShowDialog = Windows.Forms.DialogResult.OK Then

            PictureBox1.Image = Image.FromFile(opf.FileName)

        End If


    End Sub

    Private Sub BTN_INSER_Click(sender As Object, e As EventArgs) Handles BTN_INSER.Click

        Dim command As New SqlCommand("insert into Table_Images(name,description,the_image) values(@name,@desc,@img)", connection)

        Dim ms As New MemoryStream
        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

        command.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBoxName.Text
        command.Parameters.Add("@desc", SqlDbType.VarChar).Value = TextBoxDesc.Text
        command.Parameters.Add("@img", SqlDbType.Image).Value = ms.ToArray()

            connection.Open()

            If command.ExecuteNonQuery() = 1 Then

                MessageBox.Show("Image Inserted")

            Else

                MessageBox.Show("Image Not Inserted")

            End If


        connection.Close()

    End Sub
End Class

///////////////OUTPUT:

vb.net save image into database







Share this

Related Posts

Previous
Next Post »