Subscribe
TechTasks.net

The Blog is Moved to www.TechTasks.net

Store and Retrive Images From DataBase

Posted by Rajesh Kumar Chekuri on


DESIGN


In SQL SERVER

create table images (pname varchar(50),pimage image)

create procedure insertimages @pname varchar(50),@pimg image

as

begin

insert into images values(@pname,@pimg)

end

go

C# code

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Data.SqlClient;

using System.Drawing.Imaging;

using System.Drawing;

public partial class _Default : System.Web.UI.Page

{

SqlConnection con; SqlCommand cmd; FileStream fs;  SqlParameter pr; SqlDataReader dr; Byte[] baget;

protected void Page_Load(object sender, EventArgs e)

{

con = new SqlConnection("Data Source=FOCALPOINT1\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True");

con.Open();

}

protected void btnsave_Click(object sender, EventArgs e)

{

fs = new FileStream(fuimage.PostedFile.FileName, FileMode.Open, FileAccess.Read);

baget = new byte[fs.Length];

fs.Read(baget, 0, (int)fs.Length);

fs.Close();

cmd = new SqlCommand("insertimages", con);

cmd.CommandType = CommandType.StoredProcedure;

//pr = new SqlParameter("@pname", SqlDbType.VarChar);

//pr.Value = txtname.Text;

cmd.Parameters.Add("@pname",SqlDbType.Text).Value=txtname.Text ;

cmd.Parameters.Add("@pimg", SqlDbType.Image).Value = baget;

cmd.ExecuteNonQuery();

cmd.Dispose ();

Image1.ImageUrl = fuimage.PostedFile.FileName;

}

protected void btnget_Click(object sender, EventArgs e)

{

SqlCommand command = new SqlCommand("select pimage from images where pname='" + txtname.Text + "'", con);

byte[] image = (byte[])command.ExecuteScalar();

fs = new FileStream("D:\\raji\\imageup\\sample.jpg", FileMode.Create, FileAccess.Write);

// "D:\\raji\\imageup\\sample.jpg" This is temp image to store retrive image

fs.Write(image, 0, image.Length);

fs.Flush();

fs.Close();

Image1.ImageUrl = "~/sample.jpg";

//following code is display images in new page

//MemoryStream stream = new MemoryStream();

//SqlCommand command = new SqlCommand("select pimage from images where pname='" + txtname.Text + "'", con);

//byte[] image = (byte[])command.ExecuteScalar();

//stream.Write(image, 0, image.Length);

//Bitmap bitmap = new Bitmap(stream);

//Response.ContentType = "image/gif";

//bitmap.Save(Response.OutputStream, ImageFormat.Gif);

//stream.Close();

}

}

Bookmark and Share
Store and Retrive Images From DataBaseSocialTwist Tell-a-Friend

2 comments:

Anonymous said...

GOOD JOB

Anonymous said...

Great work

Subscribe to: Post Comments (Atom)
Thanks For Visiting