【发布时间】: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