如何 get/load 使用 blob 图像

How to get/load blob image using

我正在尝试加载图像、longblob 字段,并且我的 table 中有 1 行(id、设计、名称)数据。但是当我点击按钮显示图片时却没有显示。

我有一个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

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

这些行乱序了:

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)