【问题标题】:Saving an Image to MYSQL Blob field将图像保存到 MYSQL Blob 字段
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我试过 parameters.Add(new OdbcParameter("Image2", ImageData));没有返回错误,但仍然没有将图像保存到数据库中。
  • 所以,请先尝试使用 MySql 提供程序。
猜你喜欢
  • 2015-02-07
  • 2023-03-15
  • 1970-01-01
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2018-01-23
  • 2017-05-13
相关资源
最近更新 更多