用实体框架将数据库中的图像加载到gridview中
本文关键字:图像 加载 gridview 框架 数据库 实体 | 更新日期: 2023-09-27 18:12:45
我已经用实体框架从标准Northwind数据库创建了一个模型。现在我想填充一个Gridview与类别的列表,但在该类别实体(表)是一个列图片也是如此。gridview被Id, Description和CategoryName填充,但我没有得到gridview列中的图片,这是二进制数据。
有人知道解决这个问题的方法吗?
谢谢。
你可以像这样…
你必须添加新的通用处理程序-------右键单击解决方案资源管理器并添加新的通用处理程序,并将其命名为"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;
}
}
}
和默认值。添加新的Gridview控件,并使用SQLDatasource控件将其绑定
<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>
我希望它会帮助你......