简体   繁体   English

检索存储在 sql 服务器 2005 中的图像时出现问题

[英]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我正在尝试从 sql 服务器 2005 检索图像到 asp.net web 页面使用 Z240AA2CEC4B29C5EZA 在这里是我的 BEE20B29C5EZA 代码

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.问题是我的图像控件中没有任何 output。

here is my code i used to store images into database: byte[] data = ImageUpload.FileBytes;这是我用来将图像存储到数据库中的代码: 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将 URL 设置为该文件

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.发布的代码唯一明显错误的是您将ImageUrl设置为文字"Path" ,而不是存储在 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.很明显,Path 不是 URL 而是目录/文件路径,因此您需要做一些魔术来将其设置为 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.如果您不反对使用其他人的代码并且能够检查一下 - http://aspnet.codeplex.com/releases/view/16449 ,它可以工作并且很棒。

If all else fails consider writing your own ASHX or implementation of IHttpHandler to tackle this.如果一切都失败了,请考虑编写自己的 ASHX 或 IHttpHandler 的实现来解决这个问题。 A quick google turns up this http://www.dotnetperls.com/ashx一个快速的谷歌出现了这个http://www.dotnetperls.com/ashx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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