I am just not sure enough about if this is a good or a bad idea to save images in DB. The most cases I have heard about said that it’s a bad idea and in very fewer articles I have read that “No, it’s same expensive.” But my suggestion would be that, if you have to pull only a single image from DB at a time, then you can use this method. But not in the else cases. In case of web development, there is not need to do this, because PHP is already having very easy procedure to save the image or any file in server’s directory and pulling them back and so as other server side languages.
Target:
Assumption:
- Creating Database, Table in MySQL
- Adding data to MySQL database.
- Creating projects, saving projects, writing codes in VB.net.
Prerequisites:
-
The MySQL Server
-
The MySQL .NET Connector
-
An IDE for DB Management. Such as SQLyog, PHPmyadmin etc. If you don’t have any, then you can download a free tool.
- Finally, Visual Basic .NET 2010 [ 2008 will also work ]
Read Also: Using MySQL Database with Visual Basic .NET 2010
The Database:
Column Name
|
Type
|
Description
|
id
|
int
|
This is not necessary, I have just used it, and I think you should to.
|
Image_data
|
mediumblob
|
This field actually holds the image. I am taking “mediumblob” as data type as it can hold nearly 4 MB of data, that is an image with nearly 4 MB of file size.
|
The Form:
Serial
|
Control
|
Name
|
Value
|
Usage
|
1
|
Picture Box
|
pic_box_save
|
|
It holds the image that will be saved in the DB. You can set an image by default as I did.
|
2
|
Button
|
btn_save
|
Save
|
The saving procedure is underneath this button.
|
3
|
Picture Box
|
pic_box_get
|
|
It holds the image after pulling that from the DB.
|
4
|
Button
|
btn_get
|
Get
|
The procedure to pull the image from the DB lies with this button.
|
First Phase – Creating connection and declaring variables:
Imports MySql.Data
Public Class Form1
Dim sql As String
Dim sql_connection As MySqlClient.MySqlConnection
Dim sql_command As MySqlClient.MySqlCommand
Dim sql_reader As MySqlClient.MySqlDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sql_connection = New MySqlClient.MySqlConnection("Data Source=localhost;user id=root;database=my_test_projects;")
Try
sql_connection.Open()
Catch ex As Exception
MsgBox("Error Creating DB Connection")
End Try
End Sub
End Class
Second Phase – Saving the image in DB:
Double-Click the “btn_save” button to open the code editor and write the following code.
Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
Dim FileSize As UInt32
Dim mstream As New System.IO.MemoryStream()
pic_box_save.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
FileSize = mstream.Length
mstream.Close()
MsgBox(FileSize)
Try
sql = "INSERT INTO image_in_db(id, image_data) VALUES(@image_id, @image_data)"
sql_command = New MySqlClient.MySqlCommand(sql, sql_connection)
sql_command.Parameters.AddWithValue("@image_id", Nothing)
sql_command.Parameters.AddWithValue("@image_data", arrImage)
sql_command.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
MsgBox("Image has been saved.")
End Sub
Third Phase – Pulling the image back from the DB:
Double-Click the “btn_get” button to open the code editor and write the following code.
Private Sub btn_get_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_get.Click
Dim arrImage() As Byte
Try
sql = "SELECT * FROM image_in_db WHERE id = '1'"
sql_command = New MySqlClient.MySqlCommand(sql, sql_connection)
sql_reader = sql_command.ExecuteReader()
sql_reader.Read()
arrImage = sql_reader.Item("image_data")
Dim mstream As New System.IO.MemoryStream(arrImage)
pic_box_get.Image = Image.FromStream(mstream)
sql_reader.Close()
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
End Sub
The Full Source Code:
Download Full Project using the following download link:
Saving Images in MySQL DB