Store or save images in SQL Server database using C#
- Details
- Category: SQL Server
- Published on Tuesday, 25 November 2008 16:59
- Written by Shabdar
- Hits: 132461

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 table
To 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.
Following code explains how to read image file in to a byte array.
How to read image data bytes from SQL Server table
To 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.
Once you have image data in grid, get image data from grid cell. Alternatively you can also get image data from Dataset table cell.
If you want you can extend this code to save image from Picture Box to a local image file.
Points of Interest
If 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.
Requirements
Visual Studio.Net 2005
.Net Framework 2.0
MS SQL Server 2000 database or MS SQL Server 2005 database.
SpecialĀ Notes
I have published this article on codeproject.com as well. Here is the link to this article.
http://www.codeproject.com/useritems/Store_images_in_SQL_Serve.asp
Comments/Questions
|
hi thanks for nice article.
but can u tell me how to create the tables in sql server.which datatype i hav to use while creating the table? => thams (Friday 25-Apr-08 02:05 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Super..........
it worked perfect. so you continue this type issue in net!!! Thankssssssssssssssss....... => Loganathan (Friday 13-Jun-08 02:02 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
hi, this is mukhtar.
Thank you very much for this code. bye takecar :) => SARDAR MUKHTAR ALI KHAN (Thursday 19-Jun-08 06:57 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
What to write in the connection string text box? I am confused :( Kindly help me by giving an example.
=> Holy Harem (Saturday 26-Jul-08 12:54 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Hi, I want to same thing in VB6 using SQL Sever. Please help me.
=> Kashif (Tuesday 12-Aug-08 01:50 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
hi,
hru, how to insert imageurl in database(SQL Server) and retrive that imageurl in crystalreport10 and that url show the image type how get this. pls help me => vanaja (Friday 29-Aug-08 07:34 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
How to create a table using array data type in one of its field? Or any alternative way to store array in a table.
=> Shastri (Wednesday 24-Sep-08 01:55 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
Thats a very nice code but i want the same thing in VB6 using SQL Server.
=> Kashif (Friday 26-Sep-08 12:28 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Great.! Thanks LOTS!
=> Jeremy (Wednesday 29-Oct-08 11:40 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||
|
hi could u tell me whc datatype i have to use in sql for image save and and can u explain the readfile function
pls i will be very obliged thx and regards Dinesh sharma => Dinesh sharma (Tuesday 16-Dec-08 07:44 AM)
|
|||||||||||||||||||||||||||||||
| Reply | |||||||||||||||||||||||||||||||


