在C#中使用UPDATE MySQL命令

时间:2014-07-23 09:48:13

标签: c# mysql sql winforms visual-studio

假设我有一个类的方法:

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服务器版本相对应的手册,以便在''更新tblUsers SET UserImage = _binary'?PNG ...(和更新...所以,我不能真正将文件保存在数据库中。我很想发布一个关于如何在MessageBox上看到错误的图片,但我想我的账号没有给予我的特权。这样做了。

我不确定语法错误在哪里。我在想,它在@file中 - 但这只是猜测。非常感谢您的帮助。 哦,表格列UserImage的类型为LONGBLOB。

我也有兴趣了解其他事项:

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

非常感谢。

编辑:问题解决了。这么简单的事情没有给予重视。感谢所有试图提供帮助的人。我仍然愿意听取你对底部问题的看法(关于子弹的问题)。

2 个答案:

答案 0 :(得分:0)

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

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

应该更改为以下内容:

WHERE Username = " + currentUser;

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

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

答案 1 :(得分:0)

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