Monday, October 13, 2008

Store and Retrive Images From DataBase (Dot Net)

1 comments 10/13/2008
Sign up to receive our site updates!
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();

}

}


Your Ad Here

1 comments:

Anonymous said... [Reply]

GOOD JOB

Respects for your's Questions & Opinions

  • Friends
  •  

    Recent Posts

    New Comments

    Copyright 2008 All Rights Reserved Tech Tasks Template by Rajesh Kumar Chekuri