【问题标题】:cannot retrive image from sql server to vb .net form无法将图像从 sql server 检索到 vb .net 表单
【发布时间】:2017-08-22 13:01:02
【问题描述】:

我正在尝试使用 id 向用户显示图像;它抛出一个异常

从“Byte()”类型到“Byte”类型的转换无效。

当我删除图像代码时;它可以正常显示其他数据。

If rdbtninvestigator.Checked = True Then
            Dim mycmd1 As New SqlCommand("Select * From investigator where id=@id ", connection)
            mycmd1.Parameters.Add("@id", SqlDbType.VarChar).Value = txtid1.Text

            Dim table As New DataTable
            Dim adapter As New SqlDataAdapter(mycmd1)
            adapter.Fill(table)
            If table.Rows.Count > 0 Then


                lblid.Text = table.Rows(0)(0).ToString()
                lblname.Text = table.Rows(0)(1).ToString()
                lblusername.Text = table.Rows(0)(2).ToString()
                txtpassword.Text = table.Rows(0)(3).ToString()
                Dim img As Byte
                img = table.Rows(0)(4)
                Dim ms As New MemoryStream(img)
                picuser.Image = Image.FromStream(ms)

                btnupdate.Enabled = True
                btndelete.Enabled = True


            Else
                MsgBox("Account does not exist")
                btnupdate.Enabled = False
                btndelete.Enabled = False



            End If

【问题讨论】:

  • Dim img As Byte 按照你的写法,这个对象代表一个单字节,而不是一个数组。我怀疑你想要Dim img As Byte() 之类的东西(对不起,如果这不是正确的 VB 语法。但基本上你需要将它声明为一个数组而不是单个项目)

标签: vb.net sql-server-2012


【解决方案1】:

存储在数据库中的图像存储为字节数组。您收到的错误消息说您正在尝试将图像存储在字节变量中,而不是字节数组中。

您需要添加/更正的代码行是:

Dim image As Byte()

该行创建了一个Byte() 类型的变量,一个字节数组。

【讨论】:

    猜你喜欢
    • 2014-08-22
    • 2012-03-03
    • 2021-04-15
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 2011-06-28
    • 2013-11-12
    • 1970-01-01
    相关资源
    最近更新 更多