How to Save an Image in a SQL Server Database?Written by Balaji B
Most of 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 relative path to file with respect to root folder of website. .Net being 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 SQL Server database provides a data type called “image” which is used to store images in database.To access these images stored in database we will be using ADO.Net classes. To find out how to insert and retrieve an image in to SQL Server database, you can create a .aspx page which can have a HTMLInputFile control which is used to select image file that is to be saved in database. You can also create a textbox control in which you can add image name or some comment or an image id for image saved. Use a button control to upload image to database. Namespaces like System.Data.SqlClient, System.Drawing, System.Data, System.IO, and System.Drawing.Imaging are used in this task. In OnClick property of button you can write following code to upload an image to database. // create a byte[] for image file that is uploaded int imagelen = Upload.PostedFile.ContentLength; byte[] picbyte = new byte[imagelen]; Upload.PostedFile.InputStream.Read (picbyte, 0, imagelen); // Insert image and image id into database SqlConnection conn = new SqlConnection (@"give connection string here..."); try { conn.Open (); SqlCommand cmd = new SqlCommand ("insert into ImageTable " + "(ImageField, ImageID) values (@pic, @imageid)", conn); cmd.Parameters.Add ("@pic", picbyte); cmd.Parameters.Add ("@imageid", lblImageID.Text); cmd.ExecuteNonQuery (); } finally { conn.Close (); } You can also write above code in a function and call that function in OnClick event of upload button. The code given above performs following steps in process of inserting an image into database.
| | I Did Not Know I Could Do That - Nifty Software ToolsWritten by David D. Deprice
Sticky Notes http://www.deprice.com/stickynote.htm With StickyNote 9.0, you can create beautiful 3D notes on your desktop or send them instantly over Internet. StickyNote also enables you to adhere notes to documents or windows AND to attach documents to your note. StickyNote eliminates clutter and confusion of keeping up with reminders and phone numbers that never seem to be where you left them. Forget jotting down messages and let StickyNote deliver them for you. You can even customize a note's texture, color and priority with a few simple clicks, and you can set an alarm to have notes appear at a certain time or at regular intervals. Cute Reminder http://www.deprice.com/cutereminder.htm With help of Cute Reminder software you can easily create desktop sticky notes and make reminders for an appointment, birthday, phone call, bill payment, meeting, oil change, TV show and other events or tasks. Take a pill reminder, break reminder, gift reminder, etc., and you will never miss anything important. The program automatically tracks history of related events and stores information into archive. Individually colored skins, support for audio files in various formats, schedule, linking files and Web pages to reminders and printing. The user-friendly interface provides comfort at your workplace whether at office or at home.
|