Saving 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.
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.
a MemoryStream object. The code can be something like,
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.
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.
Copyright - © 2004 - 2023 - All Rights Reserved.