我有一个名为PIMAGE的字段表我无法显示此字段。 gridview命名为:dgw- 我的问题是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();
}
并且可能是ashx
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
答案 0 :(得分:0)
我假设您的意思是您在数据库中有一个图像字段。 网上有很多可用的例子,这里有asp.net forums
的例子。短版本是,您可以设置控件 在gridview中显示图像。在ImageField上,你 将使用DataImageUrlField属性来引用该名称 数据库中包含二进制图像数据的字段。也在 ImageField,使用DataImageUrlFormatString来定义路径 例如,指向ASPX处理程序页面并传递给它 DataImageUrlField中的特定ID值。
相关的代码行可能如下所示:
&lt; asp:ImageField DataImageUrlField =&#34; MyImageID&#34; DataImageUrlFormatString =&#34; MyHandlerPage.aspx theImageID = {0}&#34?; /&GT;一个 完整的示例,使用此方法并显示代码 处理程序页面位于:
http://www.highoncoding.com/Articles/207_Displaying_Images_from_Database_in_GridView.aspx
或查看this one
public void ProcessRequest (HttpContext context) { string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "Select [Content] from Images where ID =@ID"; cmd.CommandType = CommandType.Text; cmd.Connection = conn; 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代码
<asp:GridView ID="GVImages" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5"> <Columns> <asp:BoundField DataField="Name" HeaderText="Description" /> <asp:BoundField DataField="Type" HeaderText="Type" /> <asp:TemplateField HeaderText="Image"> <ItemTemplate> <asp:Image ID="Image1" runat="server" Width="200px" Height="200px" ImageUrl='<%# "ImageHandler.ashx?ID=" + Eval("ID")%>'/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
和你在aspx.cs中的page_load
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(connectionString); using (conn) { SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Images", conn); ad.Fill(dt); } GVImages.DataSource = dt; GVImages.DataBind(); } }
最后是你的web.config
<configuration> <connectionStrings> <add name="DBConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TESTDB;Trusted_Connection=yes;" providerName="System.Data.SqlClient"/> <!--<add name="BONConnection" connectionString="Data Source=XXX.com;Initial Catalog=DBNAME;User Id=UserName;Password=YourPassword;" providerName="System.Data.SqlClient" />--> </connectionStrings> ................... ...................