VB.NET: Save Image into MySQL Database

Saving image into MySQL Database is not so difficult, you must set the data type of your image column into “mediumblob”. In your save query code just insert the code below in the above part of your code.

Dim mstream As New System.IO.MemoryStream()
pb_Image.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
mstream.Close()

Remember that the pb_Image is the name of picture box, you can change it depending on your setup. As you notice that we declare the “arrImage()” as Byte so insert that code in the query portion.

mysc.Parameters.AddWithValue(“@Image_Data“, arrImage)

Insert the code above after your MySQL Command declaration. After that you must insert the “@Image_Data” into your query, like code below. Remember that “Image_Data” is the name of your image column in your MySQL Table.

‘” & txt_Type.Text & “‘,@Image_Data,'” & txt_InStock.Text & “‘)”

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz