2016-01-23 73 views
0

我有一个名为PIMAGE的字段的表,而我无法显示此字段。 GridView控件命名为:dgw- 我的问题是,在GridView不显示图像 我的代码:如何在GridView表单数据库中显示图像linq

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

更多信息可以帮助您在这里的Page_Load 。你的Person类是什么样的?什么是你绑定的领域?你的GridView代码是什么样的? –

回答

0

我假设你的意思是你在数据库中有一个图像字段。 有很多的例子在网络上此可用,这里有一个从asp.net forums

简短的版本是,你会设置GridView控件中的控制 来显示图像。在ImageField上, 将使用DataImageUrlField属性来引用数据库中包含二进制图像数据的 字段的名称。同样在 ImageField上,使用DataImageUrlFormatString来定义路径 ,例如,该路径指向ASPX处理程序页面并将DataImageUrlField中的特定ID值传递给它。

的代码中的相关行可能是这样的:

< ASP:ImageField的DataImageUrlField = “MyImageID” DataImageUrlFormatString = “?MyHandlerPage.aspx theImageID = {0}”/ >一个 完整的示例使用此接近并为您显示 代码中的处理程序页面是:

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 [email protected]"; 
    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

 
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> 
................... 
................... 
+0

我使用linq查询和我的问题是显示图像保存在可能表 – sadeq

+0

,不告诉我任何具体的?你的LINQ查询只是选择一个Person对象,我不知道Person类是什么样的? –

+0

人是一个类有很多领域,我想显示字段PIMAGE,该字段有二进制图像 – sadeq

相关问题