Click here to Skip to main content
15,177,810 members
Articles / Web Development / ASP.NET
Posted 25 Feb 2005


175 bookmarked

Save An Image Into SQL Server 2000 Database

Rate me:
Please Sign up or sign in to vote.
4.56/5 (92 votes)
25 Feb 2005CPOL2 min read
How to upload files to Web pages in ASP.NET? How to read an image from a database using ADO.NET and display it in a Web page?

Image 1


.NET is the new distributed computing platform developed by Microsoft and ASP.NET is its programming model for web development. The intent of this article is to get a good experience in developing data driven ASP.NET Web Forms applications by means of a real world application. This application will teach you how to save an image file into a database and how to retrieve it from the database. It uses ADO.NET as the data access mechanism, C# as the development language, and SQL Server 2000 as the backend database.

Overview of the Solution

Normally, images are saved in the web server folder, not in the database; this is for larger file size category. In some cases like, in bank, for example, they scan the user signatures as image files and save that file into the database.

  • Database schema

    MS SQL Server 2000 is used as the backend database for this small demonstration. And I used a special data type in SQL Server called image. The image data type is used to save the image into the database.

  • Controls used in this application
    • System.Web.UI.HtmlControls.HtmlInputFile
    • System.Web.UI.WebControls.TextBox
    • System.Web.UI.WebControls.Button
  • Namespaces used in this application:
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Data;
    using System.IO;
    using System.Drawing.Imaging;

Solution with Code

Use the HtmlInputFile class, which you can declare an instance of with an <input type="file" runat="server"/> tag. The following example is a complete ASPX file that lets a user upload an image file and a comment describing the image. The OnUpload method writes the image and the comment to a table named Pictures in a SQL Server database named iSense.

// Source Code for Save the image file into the database

public void OnUpload(Object sender, EventArgs e)
    // Create a byte[] from the input file
    int len = Upload.PostedFile.ContentLength;
    byte[] pic = new byte[len];
    Upload.PostedFile.InputStream.Read (pic, 0, len);
    // Insert the image and comment into the database
    SqlConnection connection = new 
      SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
        connection.Open ();
        SqlCommand cmd = new SqlCommand ("insert into Image " 
          + "(Picture, Comment) values (@pic, @text)", connection);
        cmd.Parameters.Add ("@pic", pic);
        cmd.Parameters.Add ("@text", Comment.Text);
        cmd.ExecuteNonQuery ();
        connection.Close ();

The above created function is called using the onClick property of a button.

How do I read an image from a database using ADO.NET and display it in a Web page?

Here, I used the web page to display the image and not any other control. The following is the code for displaying the image from the database.

private void Page_Load(object sender, System.EventArgs e)
    // Put user code to initialize the page here
    MemoryStream stream = new MemoryStream ();
    SqlConnection connection = new 
      SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
        connection.Open ();
        SqlCommand command = new 
          SqlCommand ("select Picture from Image", connection);
        byte[] image = (byte[]) command.ExecuteScalar ();   
        stream.Write (image, 0, image.Length);
        Bitmap bitmap = new Bitmap (stream);
        Response.ContentType = "image/gif";
        bitmap.Save (Response.OutputStream, ImageFormat.Gif);
        connection.Close ();
        stream.Close ();

The GDI+ functions offer a rich set of features for managing and modifying image data. This article's examples offer only a glimpse into the functionality you can leverage using the classes available in the System.Drawing and System.Drawing.Imaging namespaces. For example, you can develop applications for storing and managing image files on the Web, or you can provide a simple, easily deployed application that enables users to manipulate images.

How to run this application? First, create a virtual directory and put the project files into the virtual directory. And then change the server name, database name, and table name in the following statement.

SqlConnection connection = new SqlConnection

and publish the project to get the best results.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chief Technology Officer at Zealots
Australia Australia
No Biography provided

Comments and Discussions

QuestionRe: thank you friend, it's working Pin
Member 1076117120-Apr-14 2:58
MemberMember 1076117120-Apr-14 2:58 
GeneralMy vote of 1 Pin
mostafa3586-Feb-13 13:23
Membermostafa3586-Feb-13 13:23 
GeneralRe: My vote of 1 Pin
vivekthangaswamy24-May-13 17:39
professionalvivekthangaswamy24-May-13 17:39 
Generalthnks Pin
Member 87883404-Apr-12 23:18
MemberMember 87883404-Apr-12 23:18 
QuestionTHANK YOU. Pin
Alejandro Alas26-Mar-12 7:59
MemberAlejandro Alas26-Mar-12 7:59 
Ramkumar0619-Feb-12 1:47
MemberRamkumar0619-Feb-12 1:47 
GeneralMy vote of 3 Pin
sugumaran srinuvasan20-Oct-11 4:26
Membersugumaran srinuvasan20-Oct-11 4:26 
QuestionThanx Pin
rangachisi20-Oct-11 3:33
Memberrangachisi20-Oct-11 3:33 
QuestionSave and Open a Image into SQL 2000 using Visual Basic 6 Pin
Mian Zahid6-Oct-11 20:17
MemberMian Zahid6-Oct-11 20:17 
Questionhow to read image Pin
saravanakumar.nr19-Jul-11 21:42
Membersaravanakumar.nr19-Jul-11 21:42 
GeneralSave An Image Into SQL Server 2000 Database Pin
awadhendra tiwari23-May-11 7:53
Memberawadhendra tiwari23-May-11 7:53 
GeneralMy vote of 4 Pin
patilthegodfather24-Dec-10 0:40
Memberpatilthegodfather24-Dec-10 0:40 
GeneralMy vote of 1 Pin
afedor20-Feb-10 4:57
Memberafedor20-Feb-10 4:57 
GeneralThx, it's help alot in my assignment Pin
ragk885-Aug-09 7:46
Memberragk885-Aug-09 7:46 
GeneralI want to retrieve image from sql server 2000 using 2003 Pin
wingaurav3-May-09 5:16
Memberwingaurav3-May-09 5:16 
GeneralErorr Pin
yoyonimol23-Apr-09 17:57
Memberyoyonimol23-Apr-09 17:57 
GeneralIT WORKS!!!! Thanks Vivek! Pin
lukaz101023-Apr-09 9:58
Memberlukaz101023-Apr-09 9:58 
GeneralRe: thank you friend, it's working Pin
Member 1076117120-Apr-14 2:57
MemberMember 1076117120-Apr-14 2:57 
GeneralThank You Man Pin
minatharwat18-Mar-09 1:39
Memberminatharwat18-Mar-09 1:39 
GeneralThank You Pin
minatharwat18-Mar-09 1:38
Memberminatharwat18-Mar-09 1:38 
GeneralCode to display image in gridview Pin
T.Anil13-Mar-09 2:40
MemberT.Anil13-Mar-09 2:40 
GeneralThank you!! Pin
Amol_B16-Dec-08 4:42
professionalAmol_B16-Dec-08 4:42 
QuestionHow to display image??? Pin
smetaljuan29-Oct-08 9:01
Membersmetaljuan29-Oct-08 9:01 
GeneralStore and Retrieve image without using temp file... Pin
anandmmsoft12-Oct-08 21:41
Memberanandmmsoft12-Oct-08 21:41 
GeneralNice Article Pin
irshadmohideen30-Aug-08 4:58
Memberirshadmohideen30-Aug-08 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.