【问题标题】:Using the UPDATE MySQL Command in C#在 C# 中使用 UPDATE MySQL 命令
【发布时间】:2014-07-23 09:48:12
【问题描述】:

假设我有一个类的这个方法:

private void btnChangeImage_Click(object sender, EventArgs e)
{
    using (var openFileDialogForImgUser = new OpenFileDialog())
    {
        string location = null;
        string fileName = null;
        openFileDialogForImgUser.Filter = "Image Files (*.jpg, *.png, *.gif, *.bmp)|*.jpg; *.png; *.gif; *.bmp|All Files (*.*)|*.*"; // filtering only picture file types
        var openFileResult = openFileDialogForImgUser.ShowDialog(); // show the file open dialog box
        if (openFileResult == DialogResult.OK)
        {
            using (var formSaveImg = new FormSave())
            {
                var saveResult = formSaveImg.ShowDialog();
                if (saveResult == DialogResult.Yes)
                {
                    imgUser.Image = new Bitmap(openFileDialogForImgUser.FileName); //showing the image opened in the picturebox
                    location = openFileDialogForImgUser.FileName;
                    fileName = openFileDialogForImgUser.SafeFileName;

                    FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read); //Creating a filestream to open the image file
                    int fileLength = (int)fs.Length; // getting the length of the file in bytes
                    byte[] rawdata = new byte[fileLength]; // creating an array to store the image as bytes
                    fs.Read(rawdata, 0, (int)fileLength); // using the filestream and converting the image to bits and storing it in an array

                    MySQLOperations MySQLOperationsObj = new MySQLOperations("localhost", "root", "myPass");
                    MySQLOperationsObj.saveImage(rawdata);
                    fs.Close();
                }
                else
                    openFileDialogForImgUser.Dispose();
            }
        }
    }
}

这个方法来自另一个类(MySQLOperations):

public void saveImage(byte[] rawdata)
{
    try
    {
        string myConnectionString = "Data Source = " + server + "; User = " + user + "; Port = 3306; Password = " + password + ";";
        MySqlConnection myConnection = new MySqlConnection(myConnectionString);
        string currentUser = FormLogin.userID;
        string useDataBaseCommand = "USE " + dbName + ";";
        string updateTableCommand = "UPDATE tblUsers SET UserImage = @file WHERE Username = \'" + currentUser + "\';";
        MySqlCommand myCommand = new MySqlCommand(useDataBaseCommand + updateTableCommand, myConnection);
        myCommand.Parameters.AddWithValue("@file", rawdata);
        myConnection.Open();
        myCommand.ExecuteNonQuery();
        myConnection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

如果必须,这是我的 MySQLOperations 类的构造函数:

public MySQLOperations(string server, string user, string password)
{
    this.server = server;
    this.user = user;
    this.password = password;
}

我要做的是将图像文件(用户通过打开文件对话框选择)保存到数据库中。问题是我收到此错误:“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在 ';UPDATE tblUsers SET UserImage = _binary'?PNG ... 附近使用的正确语法(和以此类推,带有一些随机字符)。所以,我无法真正将文件保存在数据库中。我很想在 MessageBox 上发布一张关于如何看到错误的图片,但我想我的帐户没有权限这样做吧。

我不太确定语法错误在哪里。我在想,它在@file 中——但这只是一个猜测。您的帮助将不胜感激。 哦,表列 UserImage 的类型是 LONGBLOB。

其他我也想知道的事情:

  • 是否有必要为我的表添加另一列来存储 文件的大小(因为我需要检索文件 稍后显示图像)?
  • 我在方法中这样使用using语句可以吗 btnChangeImage_Click?

非常感谢。

编辑:问题解决了。这么简单的事情没有引起重视。感谢所有试图提供帮助的人。我仍然愿意听取您对底部问题(关于项目符号的问题)的意见。

【问题讨论】:

  • 为什么不像使用 @file 那样参数化 username 值?
  • 是的,是的。好点子。我应用了它,但它似乎没有解决问题。
  • 您确定 dbName 有值吗?否则,您的声明将是: USE ;UPDATE ...
  • 好的。这个简单的建议做到了。谢谢。我忽略了。非常感谢!

标签: c# mysql sql winforms visual-studio


【解决方案1】:

我认为问题出在以下代码行:

WHERE Username = \'" + currentUser + "\';"

应该改成下面这样:

WHERE Username = " + currentUser;

或者更好(避免sql注入)到以下一个:

WHERE Username = @Username";
 myCommand.Parameters.AddWithValue("@Username", currentUser);

【讨论】:

  • 似乎没有解决问题。谢谢你的尝试,我会用你的小费。
  • @Jill 你欢迎老兄。我会再看一下你的代码,如果我发现了什么,我会告诉你的。否则,我将删除我的答案。
【解决方案2】:

不要将二进制文件存储在 MySQL 表中。相反,将其保存到磁盘并将 PNG 文件的路径保存到 MySQL 数据库。此外,使用 Christos 建议来避免 SQL 注入。

【讨论】:

    猜你喜欢
    • 2012-01-17
    • 2023-04-11
    • 2021-05-05
    • 2018-10-29
    • 2019-01-29
    • 2011-06-03
    • 2021-10-20
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多