如何在运行时从sql server显示多个图像到多个picturebox

时间:2019-03-23 16:16:38

标签: vb.net

我正在尝试在表单的图片框中显示数据库中的图片结果,但是我只是不知道如何处理。这是我的代码,请对我的代码进行哪些更改以实现此目的。.预先感谢

Dim cn As SqlConnection

            cn = New SqlConnection
            cn.ConnectionString = "Data Source=ALLAYE\SQLEXPRESS;Initial Catalog=E-commerce;Integrated Security=True"

        Dim cmd As New System.Data.SqlClient.SqlCommand("use [E-commerce];SELECT ProductTB.ProductImage  FROM ProductTB WHERE ProductID = 'shoe1'")
        cn.Open()
            cmd.Connection = cn

            Dim ImgStream As New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte()))
            cmd.CommandType = CommandType.Text

            ProductImagePictureBox.Image = Image.FromStream(ImgStream)

            ImgStream.Dispose()

            cmd.Connection.Close()

我的问题是如何更新此代码,以便能够在图片框中显示多张图片

1 个答案:

答案 0 :(得分:0)

正如我在其他地方建议的那样:

let retreatMessage = 'We no longer wish to conquer your planet. It 
is full of dogs, which we do not care for.';

 // Write your code below
 const alienShip = {
    retreat () {
        console.log(retreatMessage);
    }

    takeOff () {
        console.log('Spim... Borp... Glix... Blastoff!');
    }
};

假定您已经向名为'Clear any existing images if required. For Each pictureBox In FlowLayoutPanel1.Controls.Cast(Of PictureBox)().ToArray() pictureBox.Image.Dispose() pictureBox.Dispose() Next Using connection As New SqlConnection("Data Source=ALLAYE\SQLEXPRESS;Initial Catalog=E-commerce;Integrated Security=True"), command As New SqlCommand("SELECT ProductImage FROM ProductTB", connection) connection.Open() Using reader = command.ExecuteReader() Do While reader.Read() Using stream As New MemoryStream(DirectCast(reader("ProductImage"), Byte())) Dim img = Image.FromStream(stream) Dim pictureBox As New PictureBox With {.SizeMode = PictureBoxSizeMode.AutoSize, .Image = img} FlowLayoutPanel1.Controls.Add(pictureBox) End Using Loop End Using End Using 的表单添加了FlowLayoutPanel控件。该控件将允许您在运行时添加和删除控件,它将自动处理布局。

上面的代码还清理了您已经在做的一些事情,例如从SQL代码中删除不必要的部分并添加FlowLayoutPanel1语句。它会删除您的Using子句,从而获取表中的所有记录。您可以根据需要将其更改为仅获取一些记录。数据读取器将依次读取每个读取器,并将其添加到新的WHERE中。