2016-01-23 41 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

相關問題