Storing an Image to a Database in .NET

Several weeks ago I mentioned that I store the images that the user uploads to the system into the database.

Some of you have expressed an interest in how I do that.  So I plan to cover that today.

For the purposes of this article, I’m going to assume that you’ve already got the image uploaded and scaled and that all that is left is getting it into the database.

First, you need some place to store your image.  For this, you will need a table in your database with a field of type image.

If you use a dataset as your means of accessing the dataset this will give you a field in the DataTable with a type of:

Byte Array (byte[])

So it is a simple matter of taking the byte array that our scaling function returned to us and assigning it to the field in our database that has the photo and then updating our database using the new data.

References
0

Dave Bush has been programming for 20 years. He currently blogs about .NET at http://blog.dmbcllc.com and provides .NET coaching to small and medium sized companies. Dave is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)