【问题标题】:C# saving images to MySql database as blobC# 将图像作为 blob 保存到 MySql 数据库
【发布时间】:2023-03-15 00:43:01
【问题描述】:

由于某种原因,当我尝试为用户更新图像时,我的代码失败了。图像未正确保存。例如 38 kib 的图像在数据库中保存为 13 字节。

这是我的代码:

    public void UploadImage(Image img)
    {
        OpenConnection();
        MySqlCommand command = new MySqlCommand("", conn);
        command.CommandText = "UPDATE User SET UserImage = '@UserImage' WHERE UserID = '" + UserID.globalUserID + "';";
        byte[] data = imageToByte(img);
        MySqlParameter blob = new MySqlParameter("@UserImage", MySqlDbType.Blob, data.Length);
        blob.Value = data;

        command.Parameters.Add(blob);

        command.ExecuteNonQuery();
        CloseConnection();
    }

    public byte[] imageToByte(Image img)
    {
        using (var ms = new MemoryStream())
        {
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
    }

OpenConnection 和 closeconnection 就是 conn.Open() 和 conn.Close()。

转换并没有失败:

但是在数据库中我看到了这个:

有人知道这里发生了什么吗?

【问题讨论】:

  • 您的问题是UserImage = '@UserImage',将其替换为UserImage = @UserImage 或更传统的UserImage = ?UserImage 即可。您正在发送一个字符串,因此是 10 个字节。
  • 只是附注,但您确定要保存为 blob。 stackoverflow.com/questions/1347461/…

标签: c# mysql


【解决方案1】:

替换此代码:

OpenConnection();
MySqlCommand command = new MySqlCommand("", conn);
command.CommandText = "UPDATE User SET UserImage = '@UserImage' WHERE UserID = '" + UserID.globalUserID + "';";
byte[] data = imageToByte(img);
MySqlParameter blob = new MySqlParameter("@UserImage", MySqlDbType.Blob, data.Length);
blob.Value = data;

command.Parameters.Add(blob);

command.ExecuteNonQuery();
CloseConnection();

var userImage = imageToByte(img);

OpenConnection();

var command = new MySqlCommand("", conn);

command.CommandText = "UPDATE User SET UserImage = @userImage WHERE UserID = @userId;";

var paramUserImage = new MySqlParameter("@userImage", MySqlDbType.Blob, userImage.Length);
var paramUserId = new MySqlParameter("@userId", MySqlDbType.VarChar, 256);  

paramUserImage.Value = userImage;
paramUserId.Value = UserID.globalUserID;    

command.Parameters.Add(paramUserImage);
command.Parameters.Add(paramUserId);

command.ExecuteNonQuery();  

CloseConnection();

您发送的 '@UserImage' 是一个 10 字节长的字符串,去掉引号,它应该可以工作。

以上代码还为您的两个变量which you should always do 使用了参数。

无论如何希望这对你有帮助。

【讨论】:

    【解决方案2】:

    一开始,我喜欢将 blob 保存在数据库中好一个 :)

    如果传递参数,不要将其封装在'中,因为ADO.Net/MySql不会将其识别为参数,而不是字符串:

    command.CommandText = "UPDATE User SET UserImage = @UserImage WHERE UserID = '" + UserID.globalUserID + "';";
    

    如果你开始使用参数,为什么不将UserID也作为参数传递:

    command.CommandText = "UPDATE User SET UserImage = @UserImage WHERE UserID = @userId;";
    

    这会让每个人的想法都更加清晰。

    一件重要的事情:如果您将 blob 存储在数据库中,请不要使用 select * from ...,因为您通常不想检索 blob,但您会使用星号。这会导致不必要的流量并降低性能。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-10-22
      • 2018-01-23
      • 2016-07-02
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2015-02-05
      相关资源
      最近更新 更多