Click here to Skip to main content
Licence 
First Posted 12 Sep 2007
Views 14,187
Downloads 307
Bookmarked 17 times

Image to Sql dataBase asp.net

By | 20 Sep 2007 | Article
It help users to upload images into DataBase
 
Part of The SQL Zone sponsored by
See Also

Introduction

This article explicate the method of inserting images and pictures into SQL Server database table and display it in an Asp.Net GridView control

Description

Have you seen any web application or website without images? No, you cannot. Images played a major role in web application development. Either it's a static html website or an advanced RAD application, everything is build along with images. If your application is an E-Commerce based or Image Gallery portal, definitely you have to suffer lot on saving the images in different location with different sizes and types. And it's not an easiest job to manage those unwanted and outdated images to be removed from your file server, then making backup of those images from one server location to another location. So it is clearly time consuming and hectic.
To make your task easier, this article explains you the methods of storing the images into data source. There are many advantages of saving the images into database. The main advantage is easy management of images. You can control the number and size of images stored in your server. You can remove all unnecessary images from the database in a single sql query and you can backup the image data easily. On the other hand, you should be generous of keeping sufficient memory store in your database server.

(Optional) Is there any background to this article that may be useful such as an introduction to the basic ideas presented?

Using the code

A brief description of how to use the article or code.

//
// 
Stream img_strm=file1.PostedFile.InputStream;

int img_len=file1.PostedFile.ContentLength;

string strType=file1.PostedFile.ContentType;

string strType1=file1.Type;

string strName=txtName.Text;

Byte[] imgData = new byte[img_len];

int n = img_strm.Read(imgData,0,img_len);

int result = savetodb(strName,imgData,strType);


<hr />
private int savetodb(string strName,byte[] imgData,string strType)

{ int return1=0;

try

{

// string qry="insert into image1 values ('"+file.Name+"','"+img_type+"',"+byts+")";

//SqlCommand img_cmd=new SqlCommand(qry,con);imgname varchar(12) null default('balaji'),

//imgtype varchar(6) null default('jpg'),bytles image not null)

//img_cmd.ExecuteNonQuery();

SqlCommand storeimage = new SqlCommand("INSERT INTO image1(id,img_len,imgtype,bytles) values (@id,@img_len,@imgtype,@bytles)", con); 

storeimage.Parameters.Add("@id", SqlDbType.VarChar).Value = strName;

storeimage.Parameters.Add("@img_len", SqlDbType.BigInt).Value = imgData.Length;

storeimage.Parameters.Add("@imgtype", SqlDbType.VarChar,6).Value= strType; 

storeimage.Parameters.Add("@bytles", SqlDbType.Image).Value = imgData;

con.Open();

return1 = storeimage.ExecuteNonQuery(); 

con.Close();

}

catch(Exception ex){Response.Write(ex);con.Close();}



return return1;

}

the above coding will store the data to database

Console.Write(id);

con.Open();

SqlCommand adp =new SqlCommand("select * from image1 where id='"+id+"'",con);

SqlDataReader dr=adp.ExecuteReader();

dr.Read();

Context.Response.ContentType=dr["imgtype"].ToString();

//Context.Response.ContentLength=Convert.ToInt16(dr["img_len"].ToString());

Context.Response.BinaryWrite((byte[])dr["bytles"]);<hr />

this coding part is use full to get data from database and load it into image

to set it to datalist we have to add

image id="Image2" runat="server" imageurl="%#content.aspx?id="+DataBinder.Eval(Container.DataItem,"id")%" /
             

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Balaji Manoharan

Web Developer

India India

Member

Hai this is Balaji from India,presently working as a software programmer in asp.net
using c# and vb.Net.
 
I am interested in sharing information among peoples ,I think this is one the way to spread my ideas.
I am alse have Knowledge in DataBase.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRE:Retriving and storing images in sql server 2005 using vb.net 2005 Pingroupvinay.sarmalkar1:07 27 Feb '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 21 Sep 2007
Article Copyright 2007 by Balaji Manoharan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid