www.1001TopWords.com |
How to Save an Image in a SQL Server Database?
Most of the web applications have a lot of images used in it. These images are usually stored in a web server folder and they are accessed by giving the relative path to the file with respect to the root folder of the website. .Net being the platform for distributed application now, ASP.Net can be used to store images that are small to be stored in a database like SQL Server 2000 and later versions. For this purpose the SQL Server database provides a data type called "image" which is used to store images in the database. To access these images stored in the database we will be using the ADO.Net classes. To find out how to insert and retrieve an image in to the SQL Server database, you can create a .aspx page which can have a HTMLInputFile control which is used to select the image file that is to be saved in the database. You can also create a textbox control in which you can add the image name or some comment or an image id for the image saved. Use a button control to upload the image to the database. Namespaces like System.Data.SqlClient, System.Drawing, System.Data, System.IO, and System.Drawing.Imaging are used in this task. In the OnClick property of the button you can write the following code to upload an image to the database. // create a byte[] for the image file that is uploaded You can also write the above code in a function and call that function in the OnClick event of the upload button. The code given above performs the following steps in the process of inserting an image into the database. 1. Get the content length of the image that is to be uploaded To retrieve the image from the SQL Database you can perform the following steps. 1. Create a MemoryStream object. The code can be something like, MemoryStream mstream = new MemoryStream (); 2. Create a Connection object 3. Open the connection to the database 4. Create a command object to execute the command to retrieve the image 5. Use the command object's ExecuteScalar method to retrieve the image 6. Cast the output of the ExecuteScalar method to that of byte[] byte[] image = (byte[]) command.ExecuteScalar (); 7. Write the stream mstream.Write (image, 0, image.Length); 8. Create a bitmap object to hold the stream Bitmap bitmap = new Bitmap (stream); 9. Set the content type to "image/gif" Response.ContentType = "image/gif"; 10. Use the Save method of the bitmap object to output the image to the OutputStream. bitmap.Save (Response.OutputStream, ImageFormat.Gif); 11. Close the connection 12. Close the stream mstream.Close(); Using the above steps you can retrieve and display the image from the database to the web page. You can use these algorithms and take advantage of the "image" data type available in the SQLServer 2000 database to store small images that correspond to a particular record in the table of the database. This method of storing avoids the tedious task of tracking the path of the web folder if the images are stored in a web folder. Visit A Guide to .NET for a complete introduction to .NET framework. Learn about ASP.NET, VB.NET, C# and other related technologies.
|
RELATED ARTICLES
Lightning Season: How to Avoid Data Loss We have just entered the time of year that most electrical storms occur. iPod users get the picture iPod users start to get the picture and it's turning out to be colorful fun!! Simple Overview Of Computer Computer is an electronic machine work on the instructions of human being. In other words you have to input data to get your required output. Sure it has its own intelligence which is hidden from a user, that help in accurate and speedy calculations. Why Build Your Own Computer System Have you ever thought about building your own computer system? Maybe you've heard friends talking about doing this sort of thing, but you were never sure what was involved. This article attempts to answer two questions. First, why would you want to build your own computer system? Secondly, how hard is it to do this? Image Formats: GIF, JPEG, BMP When browsing the internet you are likely to come across many different image formats. However, the most common formats are: CompuServe GIF, JPEG, and Bitmaps. Classification of Computers Computers are available in different shapes, sizes and weights, due to these different shapes and sizes they perform different sorts of jobs from one another. 5 Minute Guide to Video Editing for Beginners Getting started with video editing is very simple you only need a few items and you can be producing quality movies at home that you will be able to enjoy for many years to come. Maybe even produce a hit Hollywood movie. Improve PC Performance - 6 Tips You Must Know Are you frustrated with your PC? Faster Browsing Tips IF WE say that it was mainly because of the Windows operating system that a Computer could become a personal computer it would not be an exaggeration. The revolution is still on. Windows is far beyond what a common man presently knows and uses. 7 Reasons NOT to Take Your Laptop on Holiday! Heading off on vacation soon? 4 Easy Ways to Speed Up A Sluggish PC Computers are supposed to speed up our productivity?to help us do more in less time. What do you do when your computer is running so slow that it's keeping you from getting your work done? Before you kick it to the curb, try these easy, do-it-yourself suggestions to help your system run & perform better. Tips For Getting Technical Support Help Online Sometimes your PC will start acting strange for no apparent reason or a program which you use regularly will all of a sudden stop working as it should. This can be a frustrating experience especially if you have no idea where to go for help. If this happens to you, don't panic! Here are some simple tips which may turn your frown upside down. Repairing A Corrupt .RAR/.ZIP Archive Step 1: What Exactly are Screensavers? - part II Here are some tips on how to use screensavers: Your Computer Cant Keep Time A computer needs a certain amount of information to operate; for example, the date and time, the amount of memory installed, the number of drives and their configuration, and so on. In the early days of computers, either the user typed in this information each time the computer started, or it was set using DIP switches and jumpers. Today, computers store this information in a CMOS chip that uses a small battery to retain the information when the computer is shut off. Digital Cameras + Photo Printers = Quality Instant Photographs In the 1950's and 1960's Polaroid's instant cameras were all the rage. You could shoot a picture and have the finished print in a minute or so. The quality wasn't that great, but hey, you had your pictures now! Today with digital cameras and high-tech photo printers, we have quality instant photos virtually indistinguishable from processed prints. And unlike the old instant cameras, you have the digital equivalent of a negative for future use. How To Have Two (Multiple) Copies Of Windows Having two operating systems is not as difficult as many of us consider. It just requires little hard work and some good equipment. By equipments I mean good hard disk space and good RAM sometimes, depending upon which operating systems you are going to install. How To Increase Computer Speed And Performance - Great Tips & Tricks Many computer users are worried about their computer's performance. After spending lot of bucks, still they cannot have that charm of using PC as they had expected. Here I am going to explain some major tricks which are used by many computer users which help them to take efficient use of their CPU. Can Movie Theatres Compete with Home HD TV in the Future? The battle is heating up for market share, home movie theatre, X-Box, Playstations and virtual reality take on the big screen and theme parks. You might be surprised to learn that the war is just beginning. You won't believe how high the stakes are now and what lengths they are willing to go to get your dollar. How to Place Home Theater Speakers You have your television and home theater receiver; you just bought your new speakers and subwoofer and are ready to test things out. It's time to break out the measuring tape because precision is vital to achieving the full potential of your home theater system. For this article, we are going to focus on a 5.1 surround sound system. The term "5.1" refers to the number of speakers and subwoofers in the system respectively. So for a 5.1 surround sound system, there are five speakers (the center channel, the two front speakers, and the two rear speakers) and one subwoofer. |
© Athifea Distribution LLC - 2013 |