| Homepage | Web Directory | Article Directory | Bid Directory |
![]() Articles. News. Information. | |
Category of Information, News & Articles |
|
|
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:
Personal-Tech - Google News |
RELATED ARTICLES
More Cool Web Tricks If you ask most people what frustrates them most about the Internet, many of them will tell you "It's the little things!"It seems like all the great tricks and tips that make people exclaim "STOP! How did you do that?" never make it into any manual (at least where I can find them).My father (and co-author of "Turn Words Into Traffic") calls it the "reverse oral tradition" of computing, where children teach parents the survival skills of PC life. 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. Cisco Certification: A Survival Guide To The Cisco Cable Jungle One of the most confusing parts of beginning your Cisco studies is keeping all the cable types separate in your mind, and then remembering what they're used for. This often occurs when a CCNA or CCNP candidate starts putting together their own home practice lab, and they suddenly realize that they have the equipment to run labs, but not the cables. CCNA 640-801 Certification Primer The Cisco Certified Network Associate (CCNA) Certification is meant for career enhancement as well as gaining knowledge of the LAN/WAN technologies currently available for implementation. Hence, CCNA Certification can be obtained by IT professionals who have been a part of the industry for a considerable time and are interested in furthering their career in terms of role enhancement and salary. Tips for Buying a PC Buying Your PCBuying a PC that's right for you and your family is not all that simple task. More so if you're going to buy an unbranded or an assembled one. What's the Difference Between Plasma TVs and LCD TVs? Plasma and LCD TVs are the latest trend in home entertainment. Both offer unrivalled picture quality and resolution, while doing away with the bulkiness of the older rear-projection technologies. Is Your Web Browser Putting You At Risk? It's free, it comes with Windows and it's used by approximately 94% of the Internet population. It also poses an unacceptable security risk. Is Online DVD Rental or Pay-Per-View the Best Way to Get Your Movies? With renting methods such as online DVD rental and pay-per-view, it seems almost old-fashioned to go to the rental store. But with all the different ways to get the latest movies, which way is the best? Well, that depends on what is important to you. Flash Your nVidia Video Cards BIOS You will need the following:1. 1. 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. Cisco CCNA Candidate FAQ CCNA FAQQ. What exams do I have to take to get my CCNA?A. Help, I Need a New HDTV! (Part 5 of 5) Feeling overwhelmed in selecting a new TV? With all the choices these days, you may feel like, "Where do I start?!"In the final part of our 5 part article, we the discuss tv recommendations by room.--Bedrooms and kids rooms. Cisco Certification: Building Your Own Home Lab, Part I CCNAs and CCNA candidates hear it all the time: â??Get some hands-on experienceâ??. From my personal experience climbing the Cisco certification ladder, I can tell you firsthand that there is no learning like hands-on learning. 10 Easy Steps to Speed Up Your Computer - Without Upgrading 1. Empty the Temp directory regularly. 5 Simple Ways To Increase Your Computer Speed & Help Your Computer Run More Efficiently! If you're a computer expert, you'll probably already know about these tips (hopefully this will provide you with a little reminder). If you're not an expert, don't worry - these tips are simple, and don't require a lot of computer experience. D2X Digital SLRCoolpix 8800 Actually Refers to Two Nikon Cameras Addressing a D2X Digital SLRCoolpix 8800 search, this article provides a quick comparison of the Nikon D2X digital SLR and Coolpix 8800 cameras.Nikon D2X Professional Digital SLRCheck out a Nikon D2X review or two and quickly realize this is a world class professional digital SLR. The Benefits of the New Firefox Browser You probably heard of the new Firefox browser version 1.0 recently released by Mozilla. Cisco Certification: What To Expect On Exam Day Cisco Certification: Taking Your First Certification ExamYou've studied hard; you've practiced your configurations; you've used your flash cards over and over again; and finally, the big day is here. Your first certification exam!For many Cisco certification candidates, their first exam is the CCNA Composite exam or one of the two exams that make up the CCNA, the Introduction To Networking exam or the ICND (Interconnecting Cisco Networking Devices) exam. Are You Well Protected? Winter?the official start of the cold and flu season. Though, technically speaking, things got started a little early. Virtual Memory - What is It? I recently got an e-mail asking about virtual memory. The person who sent me the question was getting an error on random occasions from their Windows operating system stating "Your computer is low on virtual memory". |
| Home | Sitemap | |
| © 2006 eInfoJunction.com | Homepage | Web Directory | Article Directory| Bid Directory |