c# - 使用 Entity Framework 从数据库加载图像到 gridview

标签 c# asp.net entity-framework gridview webforms

我已经使用 Entity Framework 从标准 Northwind 数据库创建了一个模型。 现在我想用类别列表填充 Gridview,但在该类别中,实体(表)也是一个列图片。 gridview 充满了 Id、Description 和 CategoryName,但我没有在 gridview collumn 中得到图片,它是二进制数据。

有人知道这个的解决方案吗?

谢谢。

最佳答案

你可以这样做......

您必须添加新的通用处理程序--------右键单击解决方案资源管理器并添加新的通用处理程序并将其命名为“ImageHandler.ashx”

注意:这只是关于如何从数据库加载图像并在 gridview 中显示的示例示例

这是imagehandler.ashx中的代码

<%@ WebHandler Language="C#" %>

using System;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;

public class ImageHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context)
    {
        HttpRequest req = context.Request;          
        // string categoryID = "1";          
        string categoryID = req.QueryString["CategoryID"].ToString();          
        // Get information about the specified category          
        NorthwindDataContext db = new NorthwindDataContext();          
        var category = from c in db.Categories                         
                       where Convert.ToInt32(c.CategoryID) == Convert.ToInt32(categoryID)
                       select c.Picture;          
        int len = category.First().Length;          
        // Output the binary data          
        // But first we need to strip out the OLE header          
        int OleHeaderLength = 78;          
        int strippedImageLength = len - OleHeaderLength;          
        byte[] imagdata = new byte[strippedImageLength];          
        Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);          
        if ((imagdata) != null)          
        {              
            MemoryStream m = new MemoryStream(imagdata);              
            System.Drawing.Image image = System.Drawing.Image.FromStream(m);              
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);          
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

并在 Default.aspx 页面中添加新的 Gridview 控件并使用 SQLDatasource 控件将其绑定(bind)

<div>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" DataKeyNames="CategoryID"
        DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"
        GridLines="None">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
                InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
                SortExpression="CategoryName" />
            <asp:BoundField DataField="Description" HeaderText="Description"
                SortExpression="Description" />
            <asp:TemplateField HeaderText="Picture" SortExpression="Picture">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Picture") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%#"ImageHandler.ashx?CategoryID="+ Eval("CategoryID")  %>'/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT * FROM [Categories]"></asp:SqlDataSource>

</div>

希望对你有所帮助......

关于c# - 使用 Entity Framework 从数据库加载图像到 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7931932/

相关文章:

c# - 使用 JAVA 访问动态 AX 方法

C#解析 "(true and true) or (true or false)"

ASP.NET OAuth 存在 URL 重写问题

c# - 使用 C# 和 Fiddler 了解 HTTPS?

asp.net - 客户端 JS 和 ASP.NET 回发

javascript - 使用javascript将值从td分配给asp.net hiddenfield

c# - 使用序列化在 Entity Framework 中的两个 ObjectContext 之间复制实体

entity-framework - Entity Framework Core DbContext 继承问题与构造函数中的 DbOptions

c# - DataContext 始终返回 null 作为外键

c# - 对话链有问题