【发布时间】:2015-02-05 18:01:32
【问题描述】:
我有一个 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();
【问题讨论】:
-
不要那样做。保存图片路径。
-
我以前就是这样做的。文件的路径是保存在数据库中的路径。但是我在水晶报告上显示图像时遇到了麻烦。这就是为什么我决定保存图像本身而不是路径。
-
只要您引用正确的文件路径并从数据库中获取文件名,在报告中显示图像应该不是问题。
-
您能举个例子,说明如何使用存储在数据库中的文件路径在水晶报表 10 上显示图像吗?
标签: c# mysql image insert blob