【问题标题】:Image not showing up from database图片未从数据库中显示
【发布时间】:2019-11-16 20:25:11
【问题描述】:

我将 DataList 与 SqlDataSource 一起使用,所有内容都是预设的。图片成功存入数据库(图片以字节为单位存入数据库),在TestQuery中图片可见,所有标题和描述都显示在网页中,只是图片没有显示。

Picture Here

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource" RepeatColumns="3" RepeatDirection="Horizontal">
    <ItemTemplate>
        <asp:Image ID="Image1" runat="server" Height="200px" ImageUrl='<%# Eval("image") %>' Width="200px" />
        <br />
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("title") %>'></asp:Label>
        &nbsp;<br />
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("desc") %>'></asp:Label>
        <br />
    </ItemTemplate>
</asp:DataList>


<asp:SqlDataSource ID="SqlDataSource" 
                   runat="server" 
                   ConnectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SnorDB.mdf;
                   Integrated Security=True;
                   MultipleActiveResultSets=True;
                   Application Name="EntityFramework" 
                   ProviderName="System.Data.SqlClient" 
                   SelectCommand="SELECT [image], [title], [desc] FROM [Art]">
</asp:SqlDataSource>

【问题讨论】:

标签: sql asp.net webforms


【解决方案1】:

创建通用处理程序

public void ProcessRequest(HttpContext context)
{
    string imageid = context.Request.QueryString["id"].ToString();
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SnorDB"].ConnectionString);
    connection.Open();
    SqlCommand command = new SqlCommand("select image from [Art] where imageid=" + imageid, connection);
    SqlDataReader dr = command.ExecuteReader();
    dr.Read();
    context.Response.BinaryWrite((Byte[])dr[0]);
    connection.Close();
    context.Response.End();

}

public bool IsReusable
{
    get
    {
        return false;
    }
}

在显示网页上

ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("imageid")

通过将 id 传递给处理程序。

参考:Here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 2021-10-17
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多