Rhayezelle
Member
please help me if how can i save image on sql?
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
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
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: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: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.,!
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
what are the tools used in this code??