save image on sql?

Images get saved as binary data, which means a Byte array in VB. You can save an Image to a MemoryStream and then get a Byte array from that.

Saving Images in Databases
 
here's the code to save image in sql
requirements
>>button1 = cmdBrowse - browse image to be saved
>>button2 = cmdSave - save the image
>>label1 = label1 - image file location 'save as binary data

VB.NET:
Expand Collapse Copy
 Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
        [COLOR="#4169e1"]Try[/COLOR]
            Dim fopen As New OpenFileDialog
            fopen.FileName = ""
            fopen.Filter = "Image Files (*.jpg)|*.jpg|(*.jpeg)|*.JPEG|(*.gif)|*.gif|(*.png)|*.png|All Files (*.*)|*.*"
            fopen.ShowDialog()
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(fopen.FileName)
            label1.Text = fopen.FileName
        [COLOR="#4169e1"]Catch[/COLOR] ex [COLOR="#4169e1"]As[/COLOR] [COLOR="#00ffff"]Exception[/COLOR]

        [COLOR="#4169e1"]End Try[/COLOR]
    End Sub


VB.NET:
Expand Collapse Copy
 Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Connect()
        Try
            If label1.Text = "" Then
                label1.Text = System.Environment.CurrentDirectory.ToString & "\image_not_available120x120.gif"
            End If

            Connect()
            Dim cmd As New SqlCommand
            cmd.Connection = con
            cmd.CommandText = "insert into table1(txtID,Photo) values('" & txtID.Text"', '"label3.Text & "' )"

            cmd.ExecuteNonQuery()
            MsgBox("Photo Has been successfully saved.")
            Disconnect()

        Catch exp As Exception
            MsgBox(exp.ToString())
        End Try

        Disconnect()
 End Sub



Hope it helps your concern.. happy coding.,!
 
here's the code to save image in sql
requirements
>>button1 = cmdBrowse - browse image to be saved
>>button2 = cmdSave - save the image
>>label1 = label1 - image file location 'save as binary data

VB.NET:
Expand Collapse Copy
 Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
        [COLOR="#4169e1"]Try[/COLOR]
            Dim fopen As New OpenFileDialog
            fopen.FileName = ""
            fopen.Filter = "Image Files (*.jpg)|*.jpg|(*.jpeg)|*.JPEG|(*.gif)|*.gif|(*.png)|*.png|All Files (*.*)|*.*"
            fopen.ShowDialog()
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(fopen.FileName)
            label1.Text = fopen.FileName
        [COLOR="#4169e1"]Catch[/COLOR] ex [COLOR="#4169e1"]As[/COLOR] [COLOR="#00ffff"]Exception[/COLOR]

        [COLOR="#4169e1"]End Try[/COLOR]
    End Sub


VB.NET:
Expand Collapse Copy
 Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Connect()
        Try
            If label1.Text = "" Then
                label1.Text = System.Environment.CurrentDirectory.ToString & "\image_not_available120x120.gif"
            End If

            Connect()
            Dim cmd As New SqlCommand
            cmd.Connection = con
            cmd.CommandText = "insert into table1(txtID,Photo) values('" & txtID.Text"', '"label3.Text & "' )"

            cmd.ExecuteNonQuery()
            MsgBox("Photo Has been successfully saved.")
            Disconnect()

        Catch exp As Exception
            MsgBox(exp.ToString())
        End Try

        Disconnect()
 End Sub



Hope it helps your concern.. happy coding.,!

That's not saving an image. That's saving the path of the image file, i.e. a string. That may be desirable in some circumstances as it will keep the size of the database down but it is certainly not the same thing. Your code would only work if the application was only used on one machine or the same images were in the same location one every machine. As soon as any user moved or deleted an image file, the code will break. By storing the actual image data in the database, as I demonstrated, you immediately avoid any multi-user or file issues.
 
what are the tools used in this code??

Every type used in that code is part of the .NET Framework. Not only that, every type used in that code is declared in an assembly that is referenced by default by a VS Windows Forms Application project.
 
Back
Top