2009-11-08 204 views
-1

以前我有插入圖像到sql數據庫的問題。現在我已經解決了這個問題,並能夠在sqldatabase中插入圖像。現在我面臨着從數據庫表中檢索圖像的問題。這裏是我的檢索碼:從sql數據庫檢索圖像

showimage.ashx: 
<%@ WebHandler Language="C#" Class="ShowImage" %> 
using System; 
using System.Web; 
using System.IO; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Data; 

public class ShowImage : IHttpHandler 
{ 

    public void ProcessRequest (HttpContext context) 
    { 
     int empno; 
     if (context.Request.QueryString["empid"] != null) 
      empno = Convert.ToInt32(context.Request.QueryString["id"]); 
     else 
      throw new ArgumentException("No parameter specified"); 
     context.Response.ContentType = "image/jpeg"; 
     //context.Response.Write("Hello World"); 
     Stream strm = ShowEmpImage(empno); 
     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 ShowEmpImage(int empno) 
    { 
     string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString; 
     SqlConnection connection = new SqlConnection(conn); 
     string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID"; 
     SqlCommand cmd = new SqlCommand(sql, connection); 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.AddWithValue("@ID", empno); 
     connection.Open(); 
     object img = cmd.ExecuteScalar(); 
     try 
     { 
      return new MemoryStream((byte[])img); 

     } 
     catch 
     { 
      return null; 

     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 
    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

} 

在這一行:

**context.Response.ContentType = "image/jpeg";** 

變得異常「無參數指定」

請幫助我,我如何從數據庫中檢索表的圖像。 這裏是我的GUI:

<asp:Label ID="lblEmpName" runat="server" Text="Employee Name"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="txtEName" runat="server"></asp:TextBox> 
     <br /> 
     <asp:Label ID="lblImage" runat="server" Text="Employee Image"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:FileUpload ID="imgUpload" runat="server" /> 
     <br /> 
     <br /> 
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
      onclick="btnSubmit_Click" /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp  
    <asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label> 
    <br /> 
    <hr /> 
    <asp:Image ID="Image1" style="width:200px" Runat="server"/> 
+0

被重新標記以添加c#。 – BalusC 2009-11-08 17:19:38

回答

1

你所看到的例外實際上是一個扔到線之上:

throw new ArgumentException("No parameter specified"); 

即造成的,因爲你的要求不具備empid查詢字符串:

somepage.aspx?empid=42 

比我看不到任何東西顯然是錯誤的代碼中的其他。

+0

它返回,但沒有在圖像框中顯示 – 2012-03-05 06:20:29