C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Storing Images into Database with ASP.NET


Posted Date: 22 Jun 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Noorul AmeenMember Level: Bronze    
Rating: 1 out of 5Points: 7



Introduction

This sample helps you to store and retrieve Images in any format into MS SQL Server Database tables through ASP.NET Application. Create a table with 3 fields with a field as 'Image' datatype. Add new asp.net project and add new web form. Add a File control and name it as 'File1' and make it as Server Control. Add a Submit button.

Press F7 to view the webpage Code-behind and add the following code. The code given here is in C#.

Methods to Establish Database Connection

The method 'OpenConnection' helps to create database connection. 'Con' is connection object.


public SqlConnection Con;
public void OpenConnection()
{
string cnstr="Server=YourServerName;Database=YourDatabaseName;Uid=YourUserId;Pwd=YourPassword;";
Con=new SqlConnection(cnstr);
Con.Open();
}


Methods to Store and Retrieve Images

The method 'GetImage' gets the images that is stored in the database. The method 'InsertImage' stores Images into database.


public byte[] GetImage(string ImageId)
{
string sql="Select Img_Bin from Images where Img_Code=@ImageId";
OpenConnection();
SqlCommand cmd=new SqlCommand(sql,Con);
cmd.Parameters.Add("@ImageId",SqlDbType.Int).Value=ImageId;
cmd.Prepare();
SqlDataReader dr=cmd.ExecuteReader();
dr.Read();
return (byte[])dr["Img_Bin"];
}


public void InsertImage(string ImgCode,byte[] Images,BigInt ContentLength)
{
string sql="Insert into Pot_Images (Img_Code,Img_Bin,Img_Len) values (@ImgCode,@Images,@ImgLen)";
OpenConnection();
SqlCommand cmd=new SqlCommand(sql,Con);
cmd.Parameters.Add("@ImgCode",SqlDbType.Char,10).Value=ImgCode;
cmd.Parameters.Add("@Images",SqlDbType.Image,ContentLength).Value=Images;
cmd.Parameters.Add("@ImgLen",SqlDbType.BigInt).Value=ContentLength;
cmd.Prepare();
cmd.ExecuteNonQuery();
}


Inserting and Viewing Images from Webform
Now we can use the webform to store images into the database. Double click the Submit button and copy the code below.


byte[] ImageStr=new byte[File1.PostedFile.ContentLength];
HttpPostedFile Image=File1.PostedFile;
Image.InputStream.Read(ImageStr,0,(int)File1.PostedFile.ContentLength);
InsertImage('1',ImageStr,File1.PostedFile.ContentLength);
Response.BinaryWrite(GetImage('1'));


Explanation of the Concept
Here we take a image in JPG,BMP or any format, and store it into database. First we convert the image as binary stream with the help of InputStream.Read method. Then we pass these information to insert into the database. Then we can retrieve the binary data from database to view the Image using Response.BinaryWrite method.

Summary
I have just explained the concept of storing Images into database in a very simplified manner. Anyone can easily understand and develop to built any complex application.



Responses

Author: Noorul Ameen    22 Jun 2004Member Level: Bronze   Points : 0
Dear ameenkpn£¬
How are you!
I'm a programmer from China,and I found an article named 'Storing
Images into Database with ASP.NET',but I do not know how to display the
images in a web page,would you please tell me about it?
Thanks

B/S

Sunroll




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Various session storage facilities in ASP.NET and performance implications
Previous Resource: New features in Visual Studio 2005 "Whidbey"
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use