2009-11-08 170 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