views:

708

answers:

2

I am having difficulties when trying to insert files into a SQL Server database. I'll try to break this down as best as I can:

  1. What data type should I be using to store image files (jpeg/png/gif/etc)? Right now my table is using the image data type, but I am curious if varbinary would be a better option.

  2. How would I go about inserting the image into the database? Does Microsoft SQL Server Management Studio have any built in functions that allow insertions of files into tables? If so, how is that done? Also, how could this be done through the use of an HTML form with PHP handling the input data and placing it into the table?

  3. How would I fetch the image from the table and display it on the page? I understand how to SELECT the cell's contents, but how would I go about translating that into a picture. Would I have to have a header(Content type: image/jpeg)?

I have no problem doing any of these things with MySQL, but the SQL Server environment is still new to me, and I am working on a project for my job that requires the use of stored procedures to grab various data.

Any and all help is appreciated.

A: 

There is bulk insert util acceptable from command line bcp.exe. Read this for more information how to insert binary data into MS SQL through t-sql.

Artic
Oh, I really like the way that utility works, but it's not exactly what I need. Basically, I'm working on an application for Northern Illinois University that stores a picture of every student that lives in the residence halls. When a student tries to check in at the front desk, the person working can identify that they are in fact a student by verifying that the picture matches.I'm not going to be inserting any of those pictures in the database. I just need to be able to successfully insert a single image for testing purposes to make sure that I can retrieve it correctly.
Joe Majewski
Is there an insert that works with just a single path on the hard disk?
Joe Majewski
+2  A: 

The image type is deprecated, so don't use that.

The short answer is to use the FILESTREAM type if this is SQL 2008, or varbinary(max) if it's 2005 or earlier.

The better answer is to use the FILESTREAM type if this is SQL 2008, and not store images in the database at all otherwise. Databases aren't built for this type of thing and throwing BLOBs in there can hurt performance significantly. FILESTREAM sidesteps the issue by storing the actual data on the file system; everything else is a sub-standard solution.

As for how to read and write to a varbinary column if you choose to take this non-ideal approach, it's going to be represented as a stream in PHP. That's what you get out of a query as the column value, and I believe that's what you have to put in as a parameter.

Aaronaught
We're using SQL Server 2008, but there doesn't seem to be a `FILESTREAM` type in the drop-down list. If `varbinary` works, then that will have to do. The main concern is figuring a way to insert the image into the table. Thanks for noting that the image type is deprecated, as I was leaning towards that route. :)
Joe Majewski
@Joe Majewski: FILESTREAM is available in all versions of SQL Server 2008. Do **not** use `varbinary` when you have FILESTREAM. You won't necessarily see it in the drop-down - please see the getting started guide here: http://msdn.microsoft.com/en-us/library/bb933995.aspx
Aaronaught
Alright, I'm looking into that right now. Realize that I'm working on a copy of the "real" database because I don't have privileges to access all the data, so I'm hoping that making a filestream enabled database won't cause problems when this goes live.
Joe Majewski
This is a great discussion about FILESTREAM and also has a link to MS Research on the topic of storing images in a 2005 DB (result < 256k good, > 1mb bad, YMMV): http://msdn.microsoft.com/en-us/magazine/dd695918.aspx
Robert Wagner