0

我在 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,会产生以下错误:参数无效。

4

2 回答 2

1

如果你的 ID 字段是一个整数,看看这是否能让你克服那个错误。而不是这个:

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

尝试这个:

cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = " & ctype(int32,IDtxt).ToString
于 2013-09-25T13:56:21.737 回答
0

我有同样的问题。当 BLOB 列为空时,我得到了这个错误,然后我这样解决:对不起,我刚刚找到了这个帖子

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

    reader = cmd.ExecuteReader
    While reader.Read
        If (reader("picLen "))=0 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
于 2016-09-13T07:28:38.937 回答