如何在mysql中将多个图像检索到vb.net中的图像列表

问题描述 投票:0回答:1

我想在一个MySQL数据库中检索多个图像,以便在加载表单后将其添加到图像列表集合中。

我在表单加载事件中尝试过的事情:

cn.Open()
  Using cmd = New MySqlCommand("SELECT * FROM tbl_products", cn)
    da.SelectCommand = cmd
    Dim dt_images As New DataTable
    da.Fill(dt_images)
    For Each dr As DataRow In dt_images.Rows
      Dim img_buffer = CType(dr("IMAGE"), Byte())

      Dim img_stream As New MemoryStream(img_buffer, True)

      img_stream.Write(img_buffer, 0, img_buffer.Length)
      imglist.Images.Add(dr("image").ToString(), New Bitmap(img_stream))
      img_stream.Close()
    Next
  End Using
cn.Close()
vb.net vb.net-2010
1个回答
0
投票
Using connection As New MySqlConnection("connection string here"),
      command As New MySqlCommand("SELECT image FROM tbl_products", connection)
    connection.Open()

    Using reader = command.ExecuteReader()
        While reader.Read()
            Using stream As New MemoryStream(DirectCast(reader("image"), Byte()))
                imglist.Images.Add(Image.FromStream(stream))
            End Using
        End While
    End Using
End Using
© www.soinside.com 2019 - 2024. All rights reserved.