tags:

views:

56

answers:

3

I have a blob column in my table. How do I insert an image into it and how do I retrieve it onto the picturebox on a windows form?

+6  A: 

I recommend that you don't do this. Store it as a file on the filesystem and put the filename in the database instead.

Ignacio Vazquez-Abrams
For the edification of myself and others, why is storing the image as a blog in the db a bad idea? Is this just a MySQL issue?
Shoko
There are database engines out there that handle largish BLObs in a manner that doesn't hurt performance, but MySQL isn't one of them.
Ignacio Vazquez-Abrams
A: 
  1. It is probably best if you store your images in the filesystem instead of the database.
  2. but if you want to store in DB, you can just write the image data in as string and then retrieve it back as a string, set a header depending on the image type and then just flush it to the browser. (I did that using php and it worked)
meosoft
Hope I will have to store my images in the file system. thank you Meosoft
Albert
+2  A: 

If the image is located on your MySQL server host, you could use the LOAD_FILE() command:

INSERT INTO my_table (image_col) VALUES(LOAD_FILE('/tmp/my_image.png'));

Make sure that the file is readable by MySQL, and also make sure that your MySQL user has the FILE privilege.

To grant the FILE privilege, log in as root and execute:

GRANT FILE ON *.* TO 'mysql_user'@'localhost';

In general I would also recommend storing the image on the filesystem, and just keeping the path in MySQL. However there are cases where this is useful.

Daniel Vassallo
Thanks for your advice Daniel. Currently am storing the path to the image but I want to be able to move the database and application to another computer without worrying about the path to the images.
Albert