如何使用获取/加载blob图像

时间:2015-01-05 00:56:52

标签: asp.net vb.net blob

我试图加载一个图像,longblob的字段,我的表中有一行(id,design,name)数据。但是当我点击按钮显示图像而它没有显示时。

我有一个GetImage.aspx来调用我的方法

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        Using conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MySQLConnection").ToString())
            cmd = New MySqlCommand("SELECT design FROM mytable")
            Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
            conn.Open()
            Context.Response.Clear()
            Context.Response.ContentType = "image/jpeg"
            Context.Response.BinaryWrite(imageData)
            Context.Response.End()

        End Using

    Catch ex As Exception
    End Try
End Sub

这是我的控制按钮,用于显示默认页面中的图像

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Image1.ImageUrl = "GetImage.aspx?"
End Sub

请帮助我不明白为什么我的图片没有显示

1 个答案:

答案 0 :(得分:1)

这些行无序:

Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
conn.Open()

您需要在执行命令之前打开连接:

conn.Open()
Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())

您还需要将连接与命令相关联:

cmd = New MySqlCommand("SELECT design FROM mytable", conn)