开发者

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 co开发者_如何学JAVAde 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();


Try look here:

Storing and Retrieving Images from SQL Server using Microsoft .NET


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜