Store or save images in SQL Server database using C#
This sample code explains you how you can store or save images in SQL Server database using C#.
It uses ADO.Net System.Data.SqlClient namespace.
Images can be stored in sql server using Sql parameters.
How to Store or Save Image in SQL Server tableTo store an image in to sql server, you need to read image file into a byte array. Once you have image data in byte array, you can easily store this image data in sql server using sql parameters. Following code explains you how to do this.
How to read image data bytes from SQL Server tableTo read images from SQL Server, prepare a dataset first which will hold data from SQL Server table. Bind this dataset with a gridview control on form.
Points of InterestIf you see frmImageStore in design mode, I have placed picturebox1 into a panel. This panel's AutoScroll property is set to True and SizeMode property of PictureBox1 is set to True. This allows picturebox to resize itself to the size of original picture. When picturebox's size is more than Panel1's size, scrollbars becomes active for Panel.
How to download and run program
- Download source zip file from here . Extract in a folder.
- Restore database from SQL Database sub folder.
- If some how you can not restore provided database, you can generate necessary table using script provided in SQL Database directory.
- Open solution and change connection string on frmImagesStore form.
RequirementsVisual Studio.Net 2005
.Net Framework 2.0
MS SQL Server 2000 database or MS SQL Server 2005 database.