从sql在Image Control中显示图像

时间:2013-07-29 18:28:04

标签: asp.net sql image controls

我创建了一个表名“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();
    }
}

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

0 个答案:

没有答案