【问题标题】:How to show image in GridView form database linq如何从数据库 linq 在 GridView 中显示图像
【发布时间】:2016-04-29 20:33:45
【问题描述】:

我有一个名为 PIMAGE 字段的表,但我无法显示此字段。 名为:dgw-的gridview 我的问题是gridview不显示图像 我的代码:

   protected void Page_Load(object sender, EventArgs e)
    {
        StorDataContext stor = new StorDataContext();

        var res = (from r in stor.Products
                   where r.PID!=13
                   select new {
                       name=r.PName,date=System.DateTime.Now.ToString(),company=r.Company,price=r.Price,quantity=r.Quantity,color=r.Color,size=r.Size,weight=r.PWeight,
                       PImage = r.PID});

        GridView1.Visible = true;
        GridView1.DataSource = res.ToList();
        GridView1.DataBind();
    } 

可能会灰飞烟灭

public class ShowImg : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        Int32 pid;
        if (context.Request.QueryString["getID"] != null)
        {
            pid = Convert.ToInt32(context.Request.QueryString["getID"]);
            context.Response.ContentType = "image/jpeg";
            Stream strm = GetImage(pid);
            byte[] buffer = new byte[4096];
            int byteSeq = strm.Read(buffer, 0, 4096);
            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, 4096);
            }
        }
    }
        public Stream GetImage(int pid)
    {

        StorDataContext stor = new StorDataContext();    
        System.Data.Linq.Binary b = null;
        byte[] ibyte = null;


        Product product1 = stor.Products.First(p => p.PID == pid);

        b = product1.PImage;
        ibyte = b.ToArray();

        return new MemoryStream((byte[])ibyte);
    }

和aspx

<asp:TemplateField HeaderText="PImage">

                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/ShowImg.ashx?id=" + Eval("PImage")  %>' />
                    </ItemTemplate>

                </asp:TemplateField>

怎么了? 使用 asp.net - sql server - linq

【问题讨论】:

  • 更多信息可能会有所帮助。你的 Person 类是什么样的?你绑定的领域是什么?你的 GridView 代码是什么样的?

标签: asp.net sql-server image linq gridview


【解决方案1】:

我假设您的意思是您在数据库中有一个图像字段。 网上有很多这方面的例子,这里有一个来自asp.net forums

简短的版本是,您可以设置控件 在gridview中显示图像。在 ImageField 上,您 将使用 DataImageUrlField 属性来引用 数据库中包含二进制图像数据的字段。也在 ImageField,使用 DataImageUrlFormatString 定义路径 例如,它指向一个 ASPX 处理程序页面并将 来自 DataImageUrlField 的特定 ID 值。

相关的代码行可能如下所示:

一个 使用此方法的完整示例并向您展示代码 处理程序页面位于:

http://www.highoncoding.com/Articles/207_Displaying_Images_from_Database_in_GridView.aspx

或查看this one

公共无效ProcessRequest(HttpContext上下文){ 字符串 connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = 新的 SqlCommand(); cmd.CommandText = "从 ID =@ID 的图像中选择 [内容]"; cmd.CommandType = CommandType.Text; cmd.Connection = 连接; SqlParameter ImageID = new SqlParameter("@ID", SqlDbType.BigInt); ImageID.Value = context.Request.QueryString["ID"]; cmd.Parameters.Add(ImageID); conn.Open(); SqlDataReader dReader = cmd.ExecuteReader(); dReader.Read(); context.Response.BinaryWrite((byte[])dReader["Content"]); dReader.Close(); conn.Close(); }

和aspx代码

列>

和你在 aspx.cs 中的 page_load

protected void Page_Load(object sender, EventArgs e) { 如果(!IsPostBack) { 字符串 connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; 数据表 dt = 新数据表(); SqlConnection conn = new SqlConnection(connectionString); 使用(连接) { SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Images", conn); 广告填充(dt); } GVImages.DataSource = dt; GVImages.DataBind(); } }

最后是你的 web.config

..................... .....................

【讨论】:

  • 我使用 linq 查询,我的问题是显示保存在 may 表中的图像
  • 这没有告诉我任何具体的事情吗?您的 LINQ 查询只是选择了一个 Person 对象,我不知道 Person 类是什么样的?
  • person 是一个有很多字段的类,我想显示字段 PIMAGE 该字段有二进制图像
  • 我在上面发布的内容应该可以。基本上,您需要一个自定义处理程序,您的 gridview 中的字段将请求它 - 它将请求它作为 yoursite/imagehandler.ashx?id=someRecordId,这将依次获取图像,然后将其作为图像流回(即它执行二进制写入 - 尽管上面的代码没有设置可能更可取的内容类型。
  • 您能提供更多信息吗?以上对你不起作用怎么办?您是否尝试过使用 Fiddler 工具查看请求是否存在?确保每一行实际上是从您希望处理程序代码回答的 URL 向服务器请求图像。
猜你喜欢
  • 2012-05-27
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2012-11-25
相关资源
最近更新 更多