Home > Articles > Databases > SQL Server

Loading Images into SQL Server with C#

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
First Look at SQL Server 2005 for Developers, A

Like this article? We recommend
First Look at SQL Server 2005 for Developers, A

As your web site grows and images multiply, storing those images on the hosting server's hard drive can turn a zippy hare of a site into a pokey tortoise. (Yes, they get there eventually, but users are impatiently waiting for a "photo finish"!) Jesse Smith provides a set of simple C# scripts that you can use to pop your images into your SQL Server database, for faster image retrieval and better load balancing on your site.

Many programmers are discovering the need for a more efficient way to handle images for larger web sites. eCommerce, realty, and other types of sites use from hundreds to thousands of images, often stored on the hosting server's hard drive. As these sites become larger, they need a better method of storage—for both performance and administrative reasons. If the web site is busy accessing pictures from the hard drive or logical drive, your server becomes less responsive due to increased disk activity. Even worse, the drive becomes fragmented faster. In a clustered server scenario, where the servers are load-balanced, you still have to maintain an identical copy of the images on each server, causing tedious administration.

The best way to load images is by using a database. With this strategy, you increase performance by having user requests for images occur simultaneously instead of sequentially from the hard disk. You also have a centralized location for the images, making load-balancing easier. In this article, I'll show how you can load images into an SQL Server database from a web page by using C#.

The Upload Script

The script in Listing 1 allows you or your users to upload images to the database one at a time. With traditional ASP, we had to use a registered component (DLL) to get the job done. Now, with .NET, it's all built into the language and supported; we only need to know which namespaces to use. This script also uses a code-behind (.cs) script to do all the meaningful work. (I'll cover that script next.) The script in Listing 1 contains only two fields: one for a friendly image name and the other for the actual image.

Listing 1 User page for uploading images

1 <%@ Page language="c#" Src="UploadImage.aspx.cs" Inherits="DBImages.UploadImage" %>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3 <HTML>

4 <body bgcolor=#ffffff>

5 <form enctype="multipart/form-data" runat=server id=form1 name=form1>
6 <h3>Upload your Image</h3>
7 Enter A Friendly Name<input type=text id=txtImgName runat="server" >
8 <asp:RequiredFieldValidator id=RequiredFieldValidator1 runat="server" ErrorMessage="Required" ControlToValidate="txtImgName"></asp:RequiredFieldValidator>

9 <br>Select File To Upload:
10 <input id="UploadFile" type=file runat=server>
11 <asp:button id=UploadBtn Text="Upload Me!" OnClick="UploadBtn_Click" runat="server"></asp:button>
12 </form>

13 </body>
14 </HTML>

Line 1 loads our code-behind script, called UploadImage.aspx.cs (discussed in the next section). Line 5 uses the "multipart/form-data" encoding type for the <form> tag, telling the browser that a large amount of binary (image) data will be returned by the form. Line 8 uses the .NET RequiredFieldValidator web control. It requires the user to enter a friendly name for the image. If the user attempts to leave this field blank, the script will tell the user that a friendly name is required. Depending on what you're using the script to do, you may not even need the information in this field; it simply provides a reference to the images in a more friendly context, such as for an image library or picture album.

Line 10 uses the HtmlInputFile control. This control is part of the HTML controls library for .NET and is basically a fancy text box control that contains a Browse button; it knows that the value it will receive is a binary file. Line 11 is the Button web control, which calls a function named UploadBTn_Click when the button for the control is clicked.

Save this script using any name you want; for example, UploadImage.aspx.

  • Share ThisShare This
  • Your Account

Discussions

Upload Image
Posted Jun 18, 2009 02:01 PM by ryeck
0 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

David ChisnallCocoa Tip of the Day, 1/29/10
By David Chisnall on January 29, 2010 No Comments

Don't ignore old versions of OS X.

See All Related Blogs

Informit Network