|
Favorits •
Web Domain Directory •
ODP •
Annuaire FR •
Directorio ES •
Directory EN •
Diretório PT •
IT Katalog •
Czech Katalog •
Polski Katalog •
Maps •
Satellite Photos •
View Card
|
||
![]() |
Personal Tech |
|
|
| ||
|
|
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: |
RELATED ARTICLES
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. FTP - File Transfer Protocol Explained File Transfer Protocol (FTP) is a protocol that is part of the TCP/IP suite. It is the standard for transferring large amounts of data from server to server and from servers to clients. Computer Performance Tips Introduction In this article I will clearly spell out the most effective steps you can take in order to increase the performance of your computer and enhance your computing experience.More Memory (RAM) If your computer has less then 256 MB of memory you may want to consider adding more. Connect Your IPAQ to Linux Choose Not To ConformPalm pilots and pdas are constantly growing in the ways that people use them in their everyday lives. Having a pocket pc is becoming the standard rather than the exception. How To Buy An MP3 Player Are you looking for an MP3 player but am not sure which type and model to buy? There is a wide variety of MP3 players out there, from flash memory based players to hard drive based players. Choosing a good player from the market is no easy task. The Help Desk When you think of a help desk, what do you picture? Do you see a large desk with an elderly woman sitting behind it pencil behind the ear? Maybe it has a computer or two? Of course the person is smiling, but you know it's not going to be much help to talk to them. Now, transform this image to the computer help desk. SOBIG.F Virus Promises Ill Be Back On 21 August 2003 Symantec Security Response upgraded the W32.SOBIG. Cisco CCNA / CCNP Certification: OSPF ASBRs Explained And Illustrated When I first started studying for my CCNP, some of the concepts of OSPF really confused me. This was especially true for the ASBR, stub areas, and total stub areas. 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.They can also be classified in different ways. How To Safeguard Your Computer? The following practice, if done regularly, may help you to safeguard your computer to some extent.1. Dont Let the Internet Overstimulate Your Mind The Internet is an awesome tool, but be careful and aware that the cloud of over stimulation doesn't invade your mind.It seems we have to become aware of a new problem that is starting to invade our modern, industrial society, that being the problem of over stimulation. How to Use SQLXML to Retrieve Data from SQL Server Database? Using SQL Server 2000 and above versions you can retrieve data from SQL Server in XML format directly from the database. Many XML related features were incorporated in the SQL Server database. iPod - Learn How It Can Solve Everyday Problems All over the world, people carry with them walkmans and other music devices so that they can play their favorite music as they move around from place to place. The latest thing in the portable music devices field is the iPod, which offers a higher quality of the music being played and one of the most compact design out there. How Does a Palm Pilot Work? Just The Facts, Ma'mPalm pilots are all the rage now. Everyone has a pda or wants to get one in the near future. New Computer? Steps to Protect Your Computer Before Connecting to the Internet Getting a new computer should be an exciting and gratifying experience. After all, you'll be able to do things faster, safer and without having to worry that your old hard drive is so full that one more file will "break the camel's back. How To Recognize The Telltale Signs Of Spyware Spyware is the software that collects information about your online activities and preferences. The information is then sent to another computer with the intention of selling it to online advertisers. Mail-merging: The Principles About mail-mergingMail-merging is the process of merging variable data and fixed text.Using mail-merging, you can create individualised letters, envelopes, labels and other documents without having to laboriously create each individual one. How Do MP3 Players Work? Digital Audio Technology Revolutionizes Music Enjoyment Millions have enjoyed recorded music since 1877 when Thomas Edison invented the phonograph. Then came radio, records, tapes and CD's. Get Ahead When You Build Your Own Computer If you've been kicking around the idea of building your own computer, it actually isn't a bad idea. It's easier than you might think, and you can probably come out with a system that gives you more kick for your money, than you'd see in retail, or those made-to-order places. Choosing a Portable MP3 Player: Part 1 MP3 players are everywhere! It seems that the number of makes and models in this market is growing daily, with features and capabilities intended to appeal to just about anyone shopping for one of these devices.MP3 players have been around much longer than the Apple iPod , but there is no arguing that this one device opened the market to a much larger customer base. |