为什么以下更新查询总是更新空值?

时间:2014-07-29 05:07:59

标签: mysql vb.net

我正在尝试使用以下代码更新基于id的blob字段,但它始终插入Null

 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        openconnection()
        Dim cmd As New Odbc.OdbcCommand("UPDATE blob_table SET image=@PictureBox1 WHERE id='6'", myconnection)
        Dim fs As New System.IO.FileStream("E:\Untitled.png", IO.FileMode.Open, IO.FileAccess.Read)
        Dim b(fs.Length() - 1) As Byte
        fs.Read(b, 0, b.Length)
        fs.Close()
        Dim P As Odbc.OdbcParameter = New Odbc.OdbcParameter("@PictureBox1", Odbc.OdbcType.Image, b.Length, ParameterDirection.Input, True, 0, 0, Nothing, DataRowVersion.Current, b)
        cmd.Parameters.Add(P)
        openconnection()
        cmd.ExecuteNonQuery()
        closeconnection()
    End Sub

我检查连接,它正常工作, 图像路径是有效路径。任何人都可以帮我在查询中找到错误吗?

3 个答案:

答案 0 :(得分:1)

我认为您需要将@ PictureBox1的值设置为字节数组 - 例如byte []据我所知,图像blob类型映射到而不是byte。

答案 1 :(得分:1)

尝试使用BinaryReader填充b字节数组: 类似的东西:

Dim b As Byte()    
Dim br As New BinaryReader(fs)    
b = br.ReadBytes(CInt(fs.Length))    
br.Close()    
fs.Close()    

答案 2 :(得分:0)

你必须做这样的事情才能将图像插入数据库

Using pgFileStream As FileStream = New FileStream(productImageFilePath, FileMode.Open, FileAccess.Read)
                Using pgReader As BinaryReader = New BinaryReader(New BufferedStream(pgFileStream))
                    Dim pgByteA As Byte() = pgReader.ReadBytes(CInt(pgFileStream.Length))
                    command.CommandText = " update gtab82 set memphoto=@Image where memuuid ='" & txtmemuid.Text & "' "
                    command.Parameters.AddWithValue("@Image", pgByteA)
                    command.ExecuteNonQuery()
                End Using
End Using
  • 正在使用PostgreSQL数据库,我的图片字段为bytea