【问题标题】:Parameter not valid C# when loading blob from MySQL从 MySQL 加载 blob 时参数无效 C#
【发布时间】:2020-02-11 18:42:00
【问题描述】:

这是我的代码:

        conn.Open();
        MySqlCommand cmd; MySqlDataAdapter da;
        String qry = $"SELECT email, image FROM users WHERE nickname='{textBox1.Text}'";
        cmd = new MySqlCommand(qry, conn);
        da = new MySqlDataAdapter(cmd);
        DataTable table = new DataTable();
        da.Fill(table);
        label1.Text = table.Rows[0][0].ToString();
        byte[] img = (byte[])table.Rows[0][1];
        MemoryStream ms = new MemoryStream(img);
        pictureBox1.Image = Image.FromStream(ms);
        da.Dispose();
        conn.Close();

我需要从 Mysql 数据库上传一张 blob 图片 当我尝试这样做时,我遇到了一个问题:“参数无效” 请收下

【问题讨论】:

  • 哪一行会抛出这个错误?
  • pictureBox1.Image = Image.FromStream(ms);
  • 没有。我试过了
  • ¯\_(ツ)_/¯ No. I tried this 请将此尝试添加到您的帖子中,包括任何错误,以便我们进一步提供帮助。

标签: c# mysql


【解决方案1】:

欢迎来到 Stackoverflow。我看到您想从数据库中获取图像。首先,检查图像是否正确插入。正确的做法是将图像转换为 byte[] 数组,而不是将其作为参数发送。我就是这样做的:

static public void UpdateProfilePicture(int id,Image img)
    {
        using (MySqlConnection conn = new MySqlConnection(ConnectionString.connectionString))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand())
            {
                cmd.CommandText = "UPDATE Users SET UserProfilePicture = @img WHERE idUser = @id";
                cmd.Connection = conn;

                var userImage = ImageToByte(img);
                var paramUserImage = new MySqlParameter("@img", MySqlDbType.Blob, userImage.Length);

                paramUserImage.Value = userImage;

                cmd.Parameters.Add(paramUserImage);
                cmd.Parameters.AddWithValue("@id", id);

                cmd.ExecuteNonQuery();
            }
        }
    }

    private static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }

如果您使用 MySqlWorbench 管理您的 mysql 数据库,您可以单击插入图像的单元格,它将以字节数组格式显示在窗口中。此外,如果您通过窗口的上部选项卡,您可以看到整个图像(我认为这种方法也适用于 PhpMyAdmin)(或者您可以在互联网上搜索字节数组到图像转换器的网站)。

之后,如果一切正常,这里有一个代码示例,可以帮助您检索图像:

static public Image GetUserImageById(int id)
    {
        MySqlDataAdapter da;

        using (MySqlConnection conn = new MySqlConnection(ConnectionString.connectionString))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand())
            {
                cmd.CommandText = "SELECT UserProfilePicture FROM Users WHERE idUser = @id";
                cmd.Connection = conn;

                cmd.Parameters.AddWithValue("@id", id);

                da = new MySqlDataAdapter(cmd);

                DataTable table = new DataTable();

                da.Fill(table);

                byte[] img = (byte[])table.Rows[0][0];

                MemoryStream ms = new MemoryStream(img);

                Image image;

                if (ms.Length > 0) image = Image.FromStream(ms);
                else
                image = null;

                da.Dispose();

                return image;
            }
        }
    }

P.S:如果您要插入数据库的图像或文件的格式不是字节数组格式,那么您将无法检索它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 2014-03-19
    • 2018-07-18
    • 1970-01-01
    相关资源
    最近更新 更多