-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();

    }

HttpHandler:

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 に答える