无法将图像从sql server检索到vb.net窗体
我正在尝试使用id显示具有图像的用户;它抛出一个异常 从类型“Byte()”到类型“Byte”的转换无效 当我删除图像代码时;它可以很好地显示其他数据无法将图像从sql server检索到vb.net窗体,vb.net,sql-server-2012,Vb.net,Sql Server 2012,我正在尝试使用id显示具有图像的用户;它抛出一个异常 从类型“Byte()”到类型“Byte”的转换无效 当我删除图像代码时;它可以很好地显示其他数据 If rdbtninvestigator.Checked = True Then Dim mycmd1 As New SqlCommand("Select * From investigator where id=@id ", connection) mycmd1.Parameters.Add("@
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 image As Byte()
该行创建了一个类型为
Byte()
的变量,一个字节数组。Dim img As Byte
按照您编写它的方式,该对象表示单个字节,而不是数组。我怀疑您需要类似于Dim img As Byte()
(如果这不是正确的VB语法,很抱歉。但基本上您需要将其声明为数组而不是单个项)