0

我创建了一个表名“Images”。表结构是这样的:

ImageID int not null
ImageName nvarchar(500) null
ImagePath nvarchar(500) null

我在相关表格中成功上传了一些图片信息。现在我想在图像控件的不同页面中显示这个图像。我怎样才能做到这一点?

ASPX 页面

<form id="form1" runat="server">
<div>
    <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
    <asp:FileUpload ID="FileUpload1" runat="server" />`
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
        onclick="btnSubmit_Click" />
</div>

背后的代码

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int rst;
    string conStr = null;
    string cmdStr = null;
    string fileName = null;

    fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(Server.MapPath("images/show/" + fileName));

    conStr = "Data Source=Server\\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True";
    SqlConnection con = new SqlConnection(conStr);


    cmdStr = "Insert into Images(ImageName,ImagePath1) values (@ImageName,@ImagePath1)";
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = cmdStr;
    cmd.Connection = con;
    con.Open();

    cmd.Parameters.AddWithValue("@ImageName", fileName);
    cmd.Parameters.AddWithValue("@ImagePath1", "images/show" + fileName);
    rst = cmd.ExecuteNonQuery();
    try
    {
        if (rst > 0)
        {
            lblMsg.Text = "Success";
        }
        else
        {
            lblMsg.Text = "Not success";
        }
    }
    catch (Exception ex)
    {
        lblMsg.Text = ex.Message;
    }
    finally
    {
        con.Close();
    }
}

我想知道如何在我想显示的任何地方从数据库中检索图像。我还没有为此编写任何代码。而且我不知道如何从数据库中检索图像

4

0 回答 0