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.
|
| Author: Noorul Ameen 22 Jun 2004 | Member 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
|