You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET通过ListBox选中项从SQL读取二进制图片到PictureBox报错解决问询

核心错误原因
  • 执行顺序颠倒:查询图片的代码写在了ListBox数据源绑定之前,此时ListBox没有任何选项,SelectedIndex值为-1,查询无返回结果,执行空值转字节数组时直接抛出异常
  • 查询条件逻辑错误:用字符串类型的ItemName字段匹配整数类型的列表序号SelectedIndex,条件永远不成立,必然查不到数据
  • 重复查询数据库:GetDataFromSql方法已经将整张表的所有字段(包括Image字段)读取到本地DataTable,无需再单独建立连接查询图片
  • 缺少选中项变更监听:仅在窗体加载时读取一次图片,后续切换ListBox选中项时图片无法同步更新
修复后完整代码
' 将BindingSource声明为类级别成员,方便全局访问
Private BndSrc As New BindingSource()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' 第一步:拉取全表数据绑定所有控件
    Dim dt = GetDataFromSql()
    BndSrc.DataSource = dt
    ListBox1.DisplayMember = "ItemName"
    ListBox1.DataSource = BndSrc
    TextBox1.DataBindings.Add("Text", BndSrc, "ItemID")
    TextBox2.DataBindings.Add("Text", BndSrc, "ItemName")
    TextBox3.DataBindings.Add("Text", BndSrc, "Details")
    
    ' 第二步:加载初始选中项的图片
    LoadCurrentImage()
    
    ' 第三步:绑定选中项变更事件,切换选项时自动更新图片
    AddHandler BndSrc.PositionChanged, AddressOf BndSrc_PositionChanged
End Sub

' 封装图片加载逻辑,复用代码
Private Sub LoadCurrentImage()
    ' 无选中项直接返回
    If BndSrc.Current Is Nothing Then
        PictureBox1.Image = Nothing
        Return
    End If
    Dim currentRow As DataRow = DirectCast(BndSrc.Current, DataRowView).Row
    ' 图片字段为空时直接返回
    If IsDBNull(currentRow("Image")) Then
        PictureBox1.Image = Nothing
        Return
    End If
    ' 从当前行直接读取图片字节,无需额外查库
    Dim imgBytes As Byte() = DirectCast(currentRow("Image"), Byte())
    Using ms As New MemoryStream(imgBytes)
        PictureBox1.Image = Image.FromStream(ms)
    End Using
End Sub

' 选中项变更时触发图片刷新
Private Sub BndSrc_PositionChanged(sender As Object, e As EventArgs)
    LoadCurrentImage()
End Sub

Private Function GetDataFromSql() As DataTable
    Dim dt As New DataTable
    Using connection As New SqlConnection("Data Source=WXCQSDQSD\SQLEXPRESS;Initial Catalog=food;Integrated Security = True"),
            cmd As New SqlCommand("select * FROM Items", connection)
        connection.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    Return dt
End Function
优化说明
  • 移除了冗余的数据库查询逻辑,直接从本地已加载的DataTable中取图片数据,避免SQL注入风险同时降低数据库开销
  • 增加了空值判断逻辑,避免无选中项、图片字段为空时程序崩溃
  • 封装图片加载逻辑,代码可维护性更高
  • 绑定BindingSource的位置变更事件,切换ListBox选中项时图片自动同步更新

内容的提问来源于stack exchange,提问作者Potato Python Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 13:54:06