首页 > 解决方案 > 将图像更新到 MySQL 数据库时,无法将“System.Byte []”类型的对象转换为“System.IConvertible”类型

问题描述

我无法在 MySQL 中将图像更新到我的数据库中。

我正在使用组合框来选择用户 ID,并使用该 ID 填充该组合框以及图片框上与该 ID 关联的图像

我可以很好地添加和删除图像,但问题是当我尝试更新任何图像时出现错误“无法将'System.Byte []'类型的对象转换为'System.IConvertible'”。

我查过它,它与将字节数组传递给单个字节有关,这是不可能的。

这是我更新图像的代码:

Dim filesize As UInteger

Dim imgcliente As New MemoryStream
PictureBox1.Image.Save(imgcliente, PictureBox1.Image.RawFormat)

Dim arrImage As Byte() = imgcliente.GetBuffer
filesize = imgcliente.Length
imgcliente.Close()

这是我从 ID 中检索相关图像的代码:

cmd = New MySqlCommand("SELECT Imagem_Cliente FROM adegages.clientes WHERE ID_Cliente='" & cmb_cliente.Text & "'", con)

Dim imagedata As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())

If Not imagedata Is Nothing Then
    Using ms As New System.IO.MemoryStream(imagedata, 0, imagedata.Length)
        ms.Write(imagedata, 0, imagedata.Length)
        PictureBox1.Image = New Bitmap(Image.FromStream(ms, True))
    End Using
End If

这可能只是一些线,它可能非常容易解决,但我似乎无法弄清楚我错在哪里。

提前谢谢

编辑:

这是将记录编辑到数据库的代码

Dim atualizar_vendas As New MySqlCommand("UPDATE vendas SET ID_Venda=@ID, 
  Cliente_Venda=@CLIENTE, Data_Venda=@DATA, Qnt_Vinho_Venda=@QNT, 
  Tipo_Casta_Venda=@TIPO_CASTA, Nome_Casta_Venda=@NOME_CASTA, 
  Total_Venda=@TOTAL, Imagem_Cliente=@IMG WHERE ID_Venda = @ID", con)

        atualizar_vendas.Parameters.Add("@ID", MySqlDbType.Int32).Value = txt_id_venda.Text
        atualizar_vendas.Parameters.Add("@CLIENTE", MySqlDbType.VarChar).Value = cmb_cliente.Text
        atualizar_vendas.Parameters.Add("@DATA", MySqlDbType.VarChar).Value = mtx_data_venda.Text
        atualizar_vendas.Parameters.Add("@QNT", MySqlDbType.VarChar).Value = txt_qnt_vendida.Text
        atualizar_vendas.Parameters.Add("@TIPO_CASTA", MySqlDbType.VarChar).Value = cmb_tipo_casta.Text
        atualizar_vendas.Parameters.Add("@NOME_CASTA", MySqlDbType.VarChar).Value = cmb_nome_casta.Text
        atualizar_vendas.Parameters.Add("@TOTAL", MySqlDbType.VarChar).Value = txt_total.Text
        atualizar_vendas.Parameters.Add("@IMG", MySqlDbType.Int32).Value = arrImage

        Try
            con.Open()
            If atualizar_vendas.ExecuteNonQuery() > 0 Then
                MsgBox("Editado com sucesso!", MsgBoxStyle.Information, "AdegaGes")
                Limpar()
            End If

            con.Close()

            Retrieve_Clientes()
            Retrieve_Tipo_Casta()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "AdegaGes")
            con.Close()
        Finally
            con.Dispose()
            cmd.Dispose()
        End Try

标签: mysqlvb.netado.net

解决方案


看看这个:

atualizar_vendas.Parameters.Add("@IMG", MySqlDbType.Int32).Value = arrImage

该参数要求单个整数,而不是字节数组。你可能想要MySqlDbType.Blob那里。


推荐阅读