VB.NET:从MySQL DB中恢复图像(blob)

时间:2013-09-24 09:21:41

标签: mysql vb.net image blob memorystream

我在MySQL DB中有一个名为“Foto”的字段。这个字段是一个blob。当我使用SQL Server时,以下代码有效:

    Dim conn As New MySqlConnection
    conn.ConnectionString = ConnectionString
    Dim cmd As New MySqlCommand
    cmd.Connection = conn
    conn.Open()
    cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = '" & IDtxt.ToString & "'"
    Dim reader As MySqlDataReader

        reader = cmd.ExecuteReader
        While reader.Read
            If (IsDBNull(reader("Foto"))) Then
                frmCartaIdentitaView.pctImage.Image = Nothing
            Else
                Dim byteImage() As Byte = reader("Foto")
                Dim frmImageView stmFoto As New System.IO.MemoryStream(byteImage)
                frmImageView.pctImage.Image = Image.FromStream(stmFoto)
                frmImageView.pctImage.SizeMode = PictureBoxSizeMode.Zoom
                frmImageView.Show()
            End If
        End While

但是现在我正在使用mysql,产生了以下错误:无效参数。

2 个答案:

答案 0 :(得分:1)

如果您的ID字段是整数,请查看是否会让您超过该错误。而不是:

cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = '" & IDtxt.ToString & "'"

试试这个:

cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = " & ctype(int32,IDtxt).ToString

答案 1 :(得分:0)

我遇到了同样的问题。当BLOB列为空时,我收到此错误 我这样解决了: 对不起,我刚刚发现这篇文章

<input type="button" value="Edit" onclick="changeValue('fName'); return false;" />