VB.NET:从 MySQL DB 检索图像(blob)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18977554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:12:36  来源:igfitidea点击:

VB.NET: Retrive an image (blob) from MySQL DB

mysqlvb.netimageblobmemorystream

提问by Pinturikkio

I've a field called "Foto" in MySQL DB. This field is a blob. When I used SQL Server, the following code worked:

我在 MySQL 数据库中有一个名为“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

But now that I'm using mysql, is produced the following error: invalid parameter.

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

回答by Steve

If your ID field is an integer, see if this gets you past that error. Instead of this:

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

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

Try this:

尝试这个:

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

回答by Salvatore

I had the same problem. I got this error when the BLOB column was empty, then I solved this way: Sorry, but I found this post just now

我有同样的问题。我在 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