-1

我正在尝试从数据库中检索具有图像类型列的图像实际上我有一个表中的表我想显示图像并且从另一个表中我想检索我正在使用表单视图的其他字段

.aspx 文件

<asp:FormView runat="server" ID="ListStories" DefaultMode="ReadOnly" >
<ItemTemplate>
<table>
<tr><td><%#Eval("Subject") %></td></tr>
<tr><td><%#Eval("Story") %></td></tr>
<tr><td><%#Eval("UserName")%> <asp:Image ID="Image1" runat="server" ImageUrl='~/ShowImage.ashx?Name=<%#Eval("UserName") %>' Width="150" Height="150" /></td></tr>

</table>
</ItemTemplate>


</asp:FormView>

后面的代码:

string connString = ConfigurationManager.ConnectionStrings["Alumnidb"].ConnectionString;
    SqlConnection conn;
    SqlCommand cmdStories;
    SqlDataReader reader;


    protected void Page_Load(object sender, EventArgs e)
    {
        RetriveStories();

    }

    protected void RetriveStories()
    {

        conn = new SqlConnection(connString);
        //cmdStories = new SqlCommand("SELECT Stories.UserName, Stories.Subject, Stories.Story, UserProfile.Photo FROM Stories INNER JOIN UserProfile ON UserProfile.UserName=Stories.UserName", conn);
        cmdStories = new SqlCommand("SELECT UserName, Subject, Story FROM Stories",conn);
        conn.Open();
        reader = cmdStories.ExecuteReader();


        ListStories.DataSource = reader;
        ListStories.DataBind();

        conn.Close();

    }

Http处理程序:

public void ProcessRequest (HttpContext context) {
        byte[] buffer = null;
        string querySqlStr = "";
        if (context.Request.QueryString["Name"] != null)
        {
            querySqlStr = "SELECT Photo from UserProfile where UserName=" + context.Request.QueryString["Name"];
        }

        conn = new SqlConnection(connString);
        SqlCommand command = new SqlCommand(querySqlStr, conn);
        SqlDataReader reader = null;
        try
        {
            conn.Open();
            reader = command.ExecuteReader();
            //get the extension name of image
            while (reader.Read())
            {
                string name = reader["Photo"].ToString();
                int endIndex = name.LastIndexOf('.');
                string extensionName = name.Remove(0, endIndex + 1);
                buffer = (byte[])reader["imageContent"];
                context.Response.Clear();
                context.Response.ContentType = "image/" + extensionName;
                context.Response.BinaryWrite(buffer);
                context.Response.Flush();
                context.Response.Close();
            }
            reader.Close();

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

它显示一个表中的字段,但不显示另一个表中的图像。. 我哪里错了?您的帮助将不胜感激。. .谢谢

4

1 回答 1

0

你应该用那个

 if (context.Request.QueryString["Name"] != null)
    {
        querySqlStr = "SELECT Photo from UserProfile where UserName= @username" ;

    }

    conn = new SqlConnection(connString);
    SqlCommand command = new SqlCommand(querySqlStr, conn);.
    command.Parameters.Add("@username",context.Request.QueryString["Name"]);

但正如你所拥有的,它必须是单引号

querySqlStr = "SELECT Photo from UserProfile where UserName='" + context.Request.QueryString["Name"] + "'";

正如我在评论中所说,不要使用字符串连接。你会得到很多东西。但我认为你的问题是你试图输入ImageUrl字节 []。它只需要路径。

请参阅此http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.imageurl.aspx

如果您想在图像中加载 byte[],请遵循上述文章。 http://www.codeproject.com/Tips/445876/Auto-bind-byte-to-asp-Image

于 2013-06-27T05:40:03.223 回答