michael1234539
03-03-2008, 03:20 AM
hi..
I have found some post here about the better ways on how to save image into database and how to retrieve.
my question now:
I am using SQL server 2005.
what the sql command to save the image to the database?;)
many thanks for any reply.....
Krownet
03-04-2008, 12:44 PM
Hello,
I haven't done this in years, mainly because I have something against storing images and files to a database. Anyways, I had to go take a quick google search for the answer but what it looks like is this:
You'll need to open up a stream of the image and read the stream to an array of bytes, and then it's just a regular insert from there into the database, ensuring that the column your inserting into is of the type BLOB (when you created your table).
Basically:
INSERT INTO pictures_table (pic_name_col,pic_data_col) VALUES ('Pic Name','<ARRAY OF BYTES>');
Hope this is somewhat helpful.
Cheers,
Brian
orbitz
03-04-2008, 12:50 PM
I thought the better way was just to save the file's name and its location if needed.
Krownet
03-04-2008, 12:55 PM
Yes. I agree. Storing just the file's name and location in the database, and then having the file stored in the filesystem on the server would be a much better way of doing it.
But there are some circumstances when storing the images in the databases presents itself as a better solution for the problem.
Cheers,
datguru
03-07-2008, 01:32 PM
Hey VG chatter, could you explain a situation in which storing the file in the database would be better? Just so i know for future reference. Thanks.
Steve_Arm
03-07-2008, 03:32 PM
Here is one. Not having word writable directories for storing files.
But everyone should avoid storing image file data in the database.
Krownet
03-07-2008, 04:31 PM
Hey VG chatter, could you explain a situation in which storing the file in the database would be better? Just so i know for future reference. Thanks.
Datguru,
First is like what Steve_Arm said: Not having to worry about making sure you have an upload directory and that directory is writable (a small concern not really an issue).
Another case is the way the files are hosted. A situation may prevent itself where a company has very small amount of space for the physical files, but have a large database servers. In this case it may present itself as a better opportunity instead of changing the existing structure.
Not a great example I realize. Just know that, in general storing a file to the filesystem is a better way to go than to the database. What I meant by "some circumstances" is basically when you're working with existing architecture.
Cheers,
datguru
03-07-2008, 05:07 PM
Ahh thats great. They were great examples, thanks very much. So it's important to go file system when you can. I was just curios because I just completed a website which had a files directory and upload feature and I was just wondering if there would of been any benefit of using the DB to store the files.
Salamok
03-25-2008, 04:07 PM
Also, if the number of images per record set was very small (like 1) it would be far more tempting to utilize the database to store it.
As far as I have been able to see the most common retrieval method used to get that image out of the database involve 1 mySQL query per img tag. This can be highly inefficient if you are trying to retrieve multiple images or even displaying the same image more than once on the page.