简体   繁体   中英

problem in retrieving image stored in sql server 2005

I am trying to retrieving image from sql server 2005 into asp.net web page using c#, here is my code

SqlCommand getImageCmd = new SqlCommand("select Image from Images where ImageName = '" + getImageDropDownList.SelectedValue.ToString() + "'", con);
byte[] imageData = (byte[])getImageCmd.ExecuteScalar();

FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

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

Image1.ImageUrl = "path";
fs.Close();

problem is i am not getting any output in my Image control.

here is my code i used to store images into database: byte[] data = ImageUpload.FileBytes;

SqlCommand sc = new SqlCommand("insert into Images(ImageName,Image) values (@n, @p)", con);
sc.Parameters.AddWithValue("@p", data);
sc.Parameters.AddWithValue("@n", imageNameTextBox.Text);
sc.ExecuteNonQuery();

I think what you are trying to do is,

  1. Grab image from database,
  2. save on disk and
  3. set the URL to that file

The only thing that is obviously wrong with the posted code is that you set ImageUrl to the literal "Path" not the value stored in Path. It is also apparent that Path is not a URL but a Directory/File Path so you'll need to do some magic to set it as the URL.

If you aren't adverse to using someone else's code and are able to I would check this out - http://aspnet.codeplex.com/releases/view/16449 , it works and it is awesome.

If all else fails consider writing your own ASHX or implementation of IHttpHandler to tackle this. A quick google turns up this http://www.dotnetperls.com/ashx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM