首页 > 解决方案 > 如何使用 vb.net 代码处理来自 Microsoft SQL Server 数据库的多个空图像值

问题描述

我希望有人可以用我当前的代码为我指明正确的方向。打开程序时出现错误:

数据为空。不能对空值调用此方法或属性。

我放了一个 else 语句,btn.backgroundimage但仍然得到那个错误。

这是我的代码的样子:

Sub FillItems()
    Try
        con = New SqlConnection(cs)
        con.Open()
        Dim PictureCol As Integer = 1 ' the column # of the BLOB field
        Dim cmdText1 As String = "SELECT RTRIM(ProductName),Image from Temp_Stock_Company INNER JOIN Product ON Product.PID=Temp_Stock_Company.ProductID where ShowPOS='Yes'"
        cmd = New SqlCommand(cmdText1)
        cmd.Connection = con
        cmd.CommandTimeout = 0
        rdr = cmd.ExecuteReader()
        flpItems.Controls.Clear()
        Do While (rdr.Read())
            'Dim btn As New Button
            'btn.Text = rdr.GetValue(0)
            'btn.TextAlign = ContentAlignment.MiddleCenter
            'btn.BackColor = Color.SteelBlue
            'btn.ForeColor = Color.White
            'btn.FlatStyle = FlatStyle.Popup
            'btn.Width = 125
            'btn.Height = 60
            'btn.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            'UserButtons.Add(btn)
            'flpItems.Controls.Add(btn)
            Dim b(rdr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
            rdr.GetBytes(PictureCol, 0, b, 0, b.Length)
            Dim ms As New System.IO.MemoryStream(b)
            Dim Dflp As New FlowLayoutPanel
            Dflp.Size = New System.Drawing.Size(197, 197)
            Dflp.BackColor = Color.Black
            Dflp.BorderStyle = BorderStyle.None
            Dflp.FlowDirection = FlowDirection.TopDown
            Dim btn As New Button
            Dim btnX As New Button
            btn.Text = rdr.GetValue(0)
            btn.Width = 197
            btn.Height = 197
            If DBNull.Value.Equals(rdr(1)) = False Then
                btn.BackgroundImage = Image.FromStream(ms)
                btn.BackgroundImageLayout = ImageLayout.Stretch
            Else
                btn.BackgroundImage = My.Resources._12
                btn.BackgroundImageLayout = ImageLayout.Stretch
            End If
            btn.FlatStyle = FlatStyle.Flat
            btn.FlatAppearance.BorderSize = 0
            btn.Text = rdr.GetValue(0)
            btn.Font = New System.Drawing.Font("Segoe UI Semibold", 1.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            btn.TextAlign = System.Drawing.ContentAlignment.BottomCenter
            btn.ForeColor = System.Drawing.Color.Black
            btnX.Text = rdr.GetValue(0)
            btnX.FlatStyle = FlatStyle.Flat
            btnX.Width = 0
            btnX.Height = 0
            btnX.FlatAppearance.BorderSize = 0
            btnX.Text = rdr.GetValue(0)
            btnX.Font = New System.Drawing.Font("Segoe UI Semibold", 8.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            btnX.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
            btnX.ForeColor = Color.White
            btnX.BackColor = Color.SteelBlue
            UserButtons.Add(btn)
            UserButtons.Add(btnX)
            Dflp.Controls.Add(btn)
            Dflp.Controls.Add(btnX)
            flpItems.Controls.Add(Dflp)
            AddHandler btn.Click, AddressOf Me.btnItems_Click
            AddHandler btnX.Click, AddressOf Me.btnItems_Click
        Loop
        con.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub

标签: vb.netsql-server-2008

解决方案


您不想在更新用户界面时保持连接打开,所以我使用了DataTable在连接关闭和处理后保存您的数据。Using...End Using块将关闭并处置您的数据库对象。框架中任何显示.Dispose方法的类都应该包含在Using块中。这包括流。

加载DataTable. 检查DBNull然后才得到你的字节数组。

我使用 AdventureWorks(Microsoft 提供的示例数据库)进行测试。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim dt As New DataTable
    Using cn As New SqlConnection(My.Settings.Adventure),
                    cmd As New SqlCommand("Select Top 10 ProductPhotoID, ThumbNailPhoto From Production.ProductPhoto;", cn)
        cn.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    For Each row As DataRow In dt.Rows
        Dim btn As New Button
        If Not IsDBNull(row(1)) Then
            Dim b() As Byte = DirectCast(row(1), Byte())
            Using ms As New System.IO.MemoryStream(b)
                btn.BackgroundImage = Image.FromStream(ms)
            End Using
        Else
            btn.BackgroundImage = Image.FromFile("C:\Users\***\Desktop\Graphics\TreeFrog.jpg")
        End If
        btn.BackgroundImageLayout = ImageLayout.Stretch
        btn.Width = 50
        btn.Height = 50
        FlowLayoutPanel1.Controls.Add(btn)
    Next
End Sub

推荐阅读