How to show Image from MySQL in picture box

ev66

Member
Joined
Apr 2, 2007
Messages
9
Programming Experience
Beginner
Hi,
I have images stored in a mysql table in BLOB format. How do i show the pic in a PictureBox ?
PictureBox1.Image = myCommand.ExecuteScalar() does not work.
The connection and SELECT is ok , its just the last bit i'm stuck on.
Here is my code-

Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String

SQL = "SELECT pic FROM mytable WHERE id=2"


conn.ConnectionString = "server=" & text1.Text & ";" _
& "user id=" & text2.Text & ";" _
& "password=" & text3.Text & ";" _
& "database=mymedia"

Try
conn.Open()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
End Try


myCommand.Connection = conn
myCommand.CommandText = SQL

myAdapter.SelectCommand = myCommand

myAdapter.Fill(myData)

PictureBox1.Image = myCommand.ExecuteScalar()
dgvStatus.DataSource = myData

Thank you
 
Hi, may i suggest you don’t put your images in BLOB fields, It will only cause you trouble hands down.

It’s much better to have images in a folder on the hard disk, and just save the picture filename in the database as a simple string.

Then you can simply set the picturebox to the path that you read from database.

This is faster, Keeps your database small and gives you more control over your images via filesystem etc.

Regards

HeavenCore

PS: i am not saying using BLOB for binary storage is a bad idea, infact it has many advantages, its just easier this way.
 
That's a good point; only really seek to put images in a database if you absolutely cannot give users access to them any other way (its not always possible to put them on a hard disk, because they might be on a server that the user has no permission to do file access on)
 
Back
Top