问题
Can someone give me some advice on how to save an image file into DB in C#? I will be saving a lot of images so what's the best way to reduce the space it will be needing? I'm currently using varbinary(max) field in DB.
Thanks!
回答1:
If you MUST store the actual file in SQL then make sure you put some restrictions around the size of the image and make sure you properly plan the database sizing - how many images and of what size are you expecting?
If you must store it in SQL my preferences is to automatically have it sitting in its own SQL database. This way any performance issues I run into with these images is not affecting the rest of my database.
NOW, if you do not have to store the images in a SQL database then I think this is just calling out to be done with Azure Blob Storage. Go take a look at it - it will give you what you want in a very efficient system.
If you cannot use Azure then you should put together a class that will handle reading/writing/lookup of images to a file system. Saving to the file system (done properly) will work much better than saving to SQL.
回答2:
That would be fine. however, if these images are so big and your DBA might not like oyu anymore for putting them in there, you could place them on a drive somewhere and store the location in the DB.
I guess there are many ways you could handle this. Just weigh your pros and cons.
I just gave you one alternative.
回答3:
Not a good idea to store images in database. You can store the urls to the images.
回答4:
What format are the picture and what database technology are you using?
One option is to convert the images to a binary array and push this to your database. I found some nice code on how to accomplish this over at codeproject:
http://www.codeproject.com/KB/aspnet/ImageInDatabase.aspx
回答5:
Like Magnus said, FileStreams are not stored in the pages of a DB, so it doesn't affect performance.
Although, storing files in the DB means your DB backups will become MUCH larger.
Someone else mentioned storing just the URLs. This is also a great idea and probably the most per formant for a simple setup.
来源:https://stackoverflow.com/questions/5806159/c-sharp-saving-an-image-into-db