Continued from page 1
1. Get content length of image that is to be uploaded 2. Create a byte to store image 3. Read input stream of posted file 4. Create a connection object 5. Open connection object 6. Create a command object 7. Add parameters to command object 8. Execute sql command using ExecuteNonQuery method of command object 9. Close connection object
To retrieve image from SQL Database you can perform following steps.
1. Create a MemoryStream object. The code can be something like, MemoryStream mstream = new MemoryStream (); 2. Create a Connection object 3. Open connection to database 4. Create a command object to execute command to retrieve image 5. Use command object’s ExecuteScalar method to retrieve image 6. Cast output of ExecuteScalar method to that of byte byte image = (byte) command.ExecuteScalar (); 7. Write stream mstream.Write (image, 0, image.Length); 8. Create a bitmap object to hold stream Bitmap bitmap = new Bitmap (stream); 9. Set content type to “image/gif” Response.ContentType = "image/gif"; 10. Use Save method of bitmap object to output image to OutputStream. bitmap.Save (Response.OutputStream, ImageFormat.Gif); 11. Close connection 12. Close stream mstream.Close();
Using above steps you can retrieve and display image from database to web page.
You can use these algorithms and take advantage of “image” data type available in SQLServer 2000 database to store small images that correspond to a particular record in table of database. This method of storing avoids tedious task of tracking path of web folder if 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.