How To Edit An Image In MySQL Database Using VbNet
In This VB.Net Tutorial We Will See How To Browse Picture On Button Click And Display The Selected Picture Into A PictureBox And Insert The Image From PictureBox Into MySQL DataBase Table Using MySqlCommand With Parameters In Visual Basic.Net Programming Language And Visual Studio Editor.
Project Source Code:
Imports MySql.Data.MySqlClient
Imports System.IO
Public Class Update_MySQL_Image
Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")
' display image from mysql into picturebox
Private Sub ButtonShow_Click(sender As Object, e As EventArgs) Handles ButtonShow.Click
Dim command As New MySqlCommand("SELECT `id`, `name`, `dscp`, `pic` FROM `mypics` WHERE `id` = @ID", connection)
command.Parameters.Add("@ID", MySqlDbType.UInt64).Value = TextBoxID.Text
Dim adapter As New MySqlDataAdapter(command)
Dim table As New DataTable()
Try
adapter.Fill(table)
Dim imgByte() As Byte
If table.Rows.Count = 1 Then
TextBoxName.Text = table(0)(1)
TextBoxDesc.Text = table(0)(2)
imgByte = table(0)(3)
Dim ms As New MemoryStream(imgByte)
PictureBox1.Image = Image.FromStream(ms)
Else
MessageBox.Show("No Data Found")
TextBoxName.Text = ""
TextBoxDesc.Text = ""
PictureBox1.Image = Nothing
End If
Catch ex As Exception
MessageBox.Show("ERROR")
TextBoxName.Text = ""
TextBoxDesc.Text = ""
PictureBox1.Image = Nothing
End Try
End Sub
' button browse image and display it into picturebox
Private Sub ButtonSelect_Img_Click(sender As Object, e As EventArgs) Handles ButtonSelect_Img.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
' button edit image and other data
Private Sub ButtonEDIT_Click(sender As Object, e As EventArgs) Handles ButtonEDIT.Click
Dim update_command As New MySqlCommand("UPDATE `mypics` SET `name`=@nm,`dscp`=@dc,`pic`=@img WHERE `id` = @ID", connection)
Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
update_command.Parameters.Add("@nm", MySqlDbType.VarChar).Value = TextBoxName.Text
update_command.Parameters.Add("@dc", MySqlDbType.VarChar).Value = TextBoxDesc.Text
update_command.Parameters.Add("@ID", MySqlDbType.Int64).Value = TextBoxID.Text
update_command.Parameters.Add("@img", MySqlDbType.Blob).Value = ms.ToArray()
connection.Open()
If update_command.ExecuteNonQuery() = 1 Then
MessageBox.Show("UPDATED")
Else
MessageBox.Show("NOT UPDATED")
End If
connection.Close()
End Sub
End Class
///////////////OUTPUT:
Post A Comment:
0 comments: