How to Save an Image in a SQL Server Database?

Written by Balaji B

Continued from page 1

1. Getrepparttar content length ofrepparttar 149240 image that is to be uploaded 2. Create a byte[] to storerepparttar 149241 image 3. Readrepparttar 149242 input stream ofrepparttar 149243 posted file 4. Create a connection object 5. Openrepparttar 149244 connection object 6. Create a command object 7. Add parameters torepparttar 149245 command object 8. Executerepparttar 149246 sql command usingrepparttar 149247 ExecuteNonQuery method ofrepparttar 149248 command object 9. Closerepparttar 149249 connection object

To retrieverepparttar 149250 image fromrepparttar 149251 SQL Database you can performrepparttar 149252 following steps.

1. Create a MemoryStream object. The code can be something like, MemoryStream mstream = new MemoryStream (); 2. Create a Connection object 3. Openrepparttar 149253 connection torepparttar 149254 database 4. Create a command object to executerepparttar 149255 command to retrieverepparttar 149256 image 5. Userepparttar 149257 command object’s ExecuteScalar method to retrieverepparttar 149258 image 6. Castrepparttar 149259 output ofrepparttar 149260 ExecuteScalar method to that of byte[] byte[] image = (byte[]) command.ExecuteScalar (); 7. Writerepparttar 149261 stream mstream.Write (image, 0, image.Length); 8. Create a bitmap object to holdrepparttar 149262 stream Bitmap bitmap = new Bitmap (stream); 9. Setrepparttar 149263 content type to “image/gif” Response.ContentType = "image/gif"; 10. Userepparttar 149264 Save method ofrepparttar 149265 bitmap object to outputrepparttar 149266 image torepparttar 149267 OutputStream. bitmap.Save (Response.OutputStream, ImageFormat.Gif); 11. Closerepparttar 149268 connection 12. Closerepparttar 149269 stream mstream.Close();

Usingrepparttar 149270 above steps you can retrieve and displayrepparttar 149271 image fromrepparttar 149272 database torepparttar 149273 web page.

You can use these algorithms and take advantage ofrepparttar 149274 “image” data type available inrepparttar 149275 SQLServer 2000 database to store small images that correspond to a particular record inrepparttar 149276 table ofrepparttar 149277 database. This method of storing avoidsrepparttar 149278 tedious task of trackingrepparttar 149279 path ofrepparttar 149280 web folder ifrepparttar 149281 images are stored in a web folder.

Visit .NET Programmers Guide for a complete introduction to .NET framework. Learn about ASP.NET, VB.NET, C# and other related technologies.

** Attention Webmasters / Website Owners ** You can reprint this article on your website as long as you do not modify any of the content, and include our resource box as listed above with all links intact and hyperlinked properly.

I Did Not Know I Could Do That - Nifty Software Tools

Written by David D. Deprice

Continued from page 1

Ideal Calendar

The IDEAL Calendar allowsrepparttar user to insert an unlimited amount of occasions. The program displays a dialog and plays a sound onrepparttar 148978 selected occasions. The program runs inrepparttar 148979 system tray and contains enough options to satisfy any user. The excellent options and user-interface make this program truly ideal.


* Print calendar exactly as you see it (WYSIWYG) * Select any image for your background (JPG/GIF/BMP) * Share occasions overrepparttar 148980 network (Private and Public entries) * Create multiple calendars * Backup/Restore capabilities * Various types of recurring events * Plug-ins to add more features. (PDA Export/Import, HTML Calendar, etc...)


SpeedUpMyPC makes it easier than ever before to optimize your settings, free up resources, and remove resource hogs. With simple explanations and real-time graphs for all vital system resources, SpeedUpMyPC identifies performance bottlenecks in minutes. With a heavy-duty arsenal of optimization tools, such as RAM recovery, CPU optimization, startup management, and automatic crash recovery, SpeedUpMyPC is THE must-have utility for both beginner and power users.

David Deprice loves finding cool software; he shares his discoveries at

    <Back to Page 1 © 2005
Terms of Use