Personal Technology Information |
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.
MORE RESOURCES: Unable to open RSS Feed $XMLfilename with error HTTP ERROR: 403, exiting |
RELATED ARTICLES
What Does That Error Message Really Mean? Surf the 'Net for about 10 minutes and chances rate veryhigh that you'll encounter an error of one kind oranother.Whether the error message pops up on your own computer oron a website loaded in your browser, knowing what theerror means can help you solve the problem much fasterand avoid hours of frustration (especially in a situationwhere nothing you do will solve the problem). MobiTV and Visual Stimulation Imput for Personal Cognitive Performance Stimulus thru Caffiene or Visual Input?It appears that the Mobi TV feature for Cell Phones may in fact have some positive Bio-advantages in that it exercises the brain thru visual stimulation. We know that visual input stimulates the brain. 10 Easy Steps to Speed Up Your Computer - Without Upgrading 1. Empty the Temp directory regularly. Flow Text Around a Graphic in Microsoft Word Flow Text Around a GraphicQuestion: I have inserted a photo in my Word document but when I try to make my text go around the photo, it will only go above and below it. How can I make it go around the photo?Answer: Once you have inserted your photo or clip art, left click the object to select it, then right click it to open the shortcut menu. How To Search Your PC With Google Desktop In this era of Internet, most people are frequent users of computers. Many of us use computers through out the day, accumulating morasses of information in various formats, which include email, digital photo, word document, spreadsheet, presentation file, ebook, article, other downloaded files, music, visited web pages, and many others. 4 Computer Money-saving Tips Tip #1 -- Rebates:A rebate is not always a bargain. Computers with rebates are often close to being discontinued. 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. Have You Invited A Spy Into Your Computer? Yes, it's true. You may have inadvertently invited a spy into your computer. Seven Things to Consider When Choosing a PDA In the early days, Personal Digital Assistants (PDAs) were not much more than glorified calculators with the ability to store contact information and brief notes. Now, the line between personal computer and personal digital assistant is blurred thanks to the advanced capabilities of these useful little devices. Help, I Need a New HDTV! (Part 1 of 5) Feeling overwhelmed in selecting a new TV? With all the choices these days, you may feel like, "Where do I start?!"In part 1 of our 5 part article, we discuss the importance (and a quick rule of thumb!) of selecting a screen size appropriate for your space.---Selecting the correct size TV is an important part of your purchase decision. Flash Memory, Part I: MMC and SD MMC and SDFlash memory is available in so many formats that it can be difficult to know what will work with any particular device. Devices such as MP3 players, PDAs, mobile phones, digital cameras, and personal computers can take advantage of flash memory to bolster their storage capacity, but selecting the right format may be easier said than done. Toshiba Wins Hollywood Support for the HD DVD format What led them to this choice, as all four studios stated separately, is better performance for the price and copyright protection.The other standard in competition, the Blu-ray disc, unconditionally backed by Sony Pictures Entertainment, is also counting on support from Metro Goldwyn Mayer and Twentieth Century Fox (the latter has recently joined the Blu-Ray Disc Association Board). DVD The Ultimate Consumer Digital Media Digital Video Disc or as it is sometimes referred to Digital Versatile Disc (DVD) has for the last several years been Hollywood's primary video delivery method and with good cause. This inexpensive, compact media delivers excellent video and audio quality. MCSE 70-290 Certification Primer Microsoft Certifications are one of the most widely acclaimed, pursued, and achieved technical certifications in the IT industry. The demand for Microsoft Certified Professionals in the job market is increasing every year. How to Buy a Plasma Television Set Most people think that all you have to do to purchase a new plasma television set is to walk into a shop, look around, and purchase the first set that catches your attention. This is how you're supposed to do it, right? Wrong! Buying a television, whether it has a plasma monitor or a LCD monitor, takes careful planning. Gain Control Over Your Screensavers With These Simple Steps We all enjoy our favorite screensavers but in the same time we all hate to see that they sometimes disturb our movies, presentations and all the things we do and do not like to be disturbed from. Just read carefully this article and you will never have this problem again. Passwords and the Human Factor Passwords have a strange dual nature. The stronger and safer the password the more likely it will be undermined by human weakness. Windows Task Manager in English You use Ctrl+Alt+Del to see what's running on your PC, to close crashed programs and processes, and to check performance. You probably avoid a few processes whose names mean nothing to you, but they're essential to Windows. A Peek Into the Near Future of Electronics Technology How long do you think DVDs have around? 20 years? 10 years? Actually, they have only been around for about seven years, but it seems like they have been around much longer. Many of us can hardly remember life before DVDs. PC Owners - The Largest Criminal Gang Ever? The year is 1981. IBM has just released the Personal Computer; a low cost machine it hopes will create a winning brand. |
home | site map | contact us |