gpt4 book ai didi

c# - 将图像保存到 MYSQL Blob 字段

转载 作者:行者123 更新时间:2023-11-29 23:20:30 25 4
gpt4 key购买 nike

我有一个 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();

最佳答案

有MySql provider对于 Visual Studio 而不是 Odbc。您可以像这样添加参数:

 parameters.Add(new MySqlParameter("Image2", ImageData));

关于c# - 将图像保存到 MYSQL Blob 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27351938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com