我有一个C#Windows应用程序,它将员工数据存储到MYSQL数据库中,包括他们的图片。但尝试将图像文件插入MYSQL Blob字段时,图片不会保存。
请帮忙。这是代码。
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "\\\\192.168.13.6\\ID Pictures";
openFileDialog1.Filter = "jpg files (*.jpg)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
int size = -1;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
file = openFileDialog1.FileName;
try
{
pnl_Picture.BackgroundImage = Image.FromFile(file);
}
catch (IOException)
{
}
}
conn.Open();
FileStream fs;
BinaryReader br;
byte[] ImageData;
fs = new FileStream(file, FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);
ImageData = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
OdbcCommand command = new OdbcCommand("INSERT INTO tbl_employee (Picture) VALUES ('"+?Image2+"')", conn);
OdbcParameterCollection parameters = command.Parameters;
parameters.Add("?Image2", OdbcType.Image);
parameters["?Image2"].Value = ImageData;
command.ExecuteNonQuery();
conn.Close();
答案 0 :(得分:0)
Visual Studio有MySql provider而不是Odbc。 您可以添加如下参数:
parameters.Add(new MySqlParameter("Image2", ImageData));