如何从数据库中显示图像?

时间:2013-03-04 04:36:14

标签: asp.net

我也知道这个(某种)问题已被多次提出并回答。我以前曾经问过这个问题。但我还是成功了。如何在特定员工ID的图像控件中显示图像...继承我的代码:

`         绑定()         GridView1.Visible =“True”

    Dim strQuery As String = "SELECT Image FROM EmployeeTable WHERE EmployeeID =@EmployeeID"
    Dim cmd As SqlCommand = New SqlCommand(strQuery)
    cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value() = Convert.ToInt32(Request.QueryString("ImageID"))
    Dim dt As DataTable = GetData(cmd)
    If dt IsNot Nothing Then
        **Dim bytes() As Byte = CType(dt.Rows(1)("Image"), Byte())**
        Response.Buffer = True
        Response.Charset = ""
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = dt.Rows(0)("ContentType").ToString()
        Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows(1)("Name").ToString())
        Response.BinaryWrite(bytes)
        Response.Flush()
        Response.End()
    End If` 

以粗体显示错误。 “位置1没有排”...... 实际上我想获取(显示)特定员工ID的图像。它如何与行位置有关?我不知道为什么我写了那行代码。无论如何,我知道有些人可以帮助我,所以我提前感谢他们......

好的,这是我将代码添加到数据库的代码。这很好。

 Dim imageData As Byte() = New Byte(FileUpload1.FileContent.Length) {}
    FileUpload1.FileContent.Read(imageData, 0, Convert.ToInt32(FileUpload1.FileContent.Length))

    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)
    con.Open()
    If EmployeeIDTextBox.Text = "" Then
        MsgBox("Please Enter EmployeeID to Add Photo")
    Else
        Dim com As New SqlCommand("UPDATE EmployeeTable SET IMAGE = @IM where EmployeeID='" & EmployeeIDTextBox.Text & "'", con)
        Dim filePath As String = Server.MapPath(FileUpload1.FileName)
        'Dim imageData As Byte() = File.ReadAllBytes(filePath)
        com.Parameters.AddWithValue("@IM", imageData)
        com.ExecuteNonQuery()
        MsgBox("File Saved Successfully!")
    End If
End Sub

现在这是我的检索代码&在图像控件中显示相同的图像......

  bind()
    GridView1.Visible = "True"
    'If Request.QueryString("ImageID") IsNot Nothing Then
    Dim strQuery As String = "SELECT Image FROM EmployeeTable WHERE EmployeeID =@EmployeeID"
    Dim cmd As SqlCommand = New SqlCommand(strQuery)
    cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value() = Convert.ToInt32(Request.QueryString("Image"))
    Dim dt As DataTable = GetData(cmd)
    If dt IsNot Nothing Then
        Dim bytes() As Byte = CType(dt.Rows(0)("Image"), Byte())
        Response.Buffer = True
        Response.Charset = ""
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = dt.Rows(0)("ContentType").ToString()
        Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows(1)("Name").ToString())
        Response.BinaryWrite(bytes)
        Response.Flush()
        Response.End()
    End IF

这不起作用......

2 个答案:

答案 0 :(得分:0)

数组索引从0开始

使用

Dim bytes() As Byte = CType(dt.Rows(0)("Image"), Byte())

你怎么不知道为什么写这个?

您拥有的代码将提示用户将图像文件保存在他的电脑上 它不会在图像控件中显示图像。

答案 1 :(得分:0)

你可以使用Http Handler并在ProcessRequest()

中使用以下代码
int Id = Convert.ToInt32(Request.QueryString["ImgId"]);
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand(
"SELECT ImageFile FROM ImageTable WHERE ImageId = @ImgId", conn))
{
    command.Parameters.Add("@ImgId", SqlDbType.Int).Value = Id ;
conn.Open();
Response.ContentType = "image/gif"; //u can set it per ur requirement
Response.BinaryWrite((byte[]) command.ExecuteScalar());
}

然后在页面中使用图片:

  <asp:Image id="Img_Db" runat="server" ImageUrl="Image.ashx?ImgId=1"/>