将图像加载到C#,然后插入MySQL表

将图像加载到C#,然后插入MySQL表,c#,mysql,C#,Mysql,我需要的是:我正在制作一个库存控制软件,但我必须在数据中上传产品的图像。我正在使用C#和MySQL 我不知道如何将加载到pictureBox中的图像放到MySQL表中。 有什么想法吗?我在这里完全迷路了 我使用以下代码将图像加载到pictureBox中: using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Open Image";

我需要的是:我正在制作一个库存控制软件,但我必须在数据中上传产品的图像。我正在使用C#和MySQL

我不知道如何将加载到pictureBox中的图像放到MySQL表中。 有什么想法吗?我在这里完全迷路了

我使用以下代码将图像加载到pictureBox中:

using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Title = "Open Image";
                dlg.Filter = "All Files (*.*)|*.*";

                if (dlg.ShowDialog() == DialogResult.OK)
                {                                     
                    pictureBox1.Image = new Bitmap(dlg.FileName);
                }
            }
如何将加载的映像插入MySQL表?我想表中的image属性应该是:

`img` LONGBLOB NOT NULL
然后,如何从MySQL将图像回调到pictureBox?我(再次)假设查询是这样的:

select img from table_name where id = '';
最后,当我进行查询时,如何从MySQL将图像加载到pictureBox


非常感谢。

我将提供两种解决方案。第一种解决方案是将原始图像以字节为单位直接存储在数据库中。第二种解决方案是我个人推荐的,即改为使用数据库中图像文件的路径

下面是一篇文章的摘录,它提出了一些关于是否BLOB的优秀观点

好吧,有几个原因不应该存储二进制数据 在数据库中:

  • 在SQL数据库中存储数据的全部意义在于对数据进行某种排序和结构,并能够 搜索这些数据。但是你如何搜索一个数据库的二进制数据呢 照片

  • 对于大型数据集,存储二进制数据将迅速增大数据库文件的大小,从而使控制数据大小变得更加困难 数据库

  • 为了在数据库中存储二进制数据,您必须不断地转义和取消转义数据,以确保没有任何中断

  • 在文件系统中存储图像的检索速度略快。现在,以下是您应该这样做的一些原因:

有一个很好的理由可以解释为什么您需要存储二进制数据 在数据库中:

  • 复制。将图像存储在数据库中可以将所有数据集中存储,这样更便于携带和使用 复制
以下是如何选择图像文件:

using (var openFileDialog = new OpenFileDialog())
{
   openFileDialog.Title = "Choose Image File";
   openFileDialog.InitialDirectory =
                Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
   openFileDialog.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
   openFileDialog.Multiselect = false;
   if (openFileDialog.ShowDialog() == DialogResult.OK)
   {
       pictureBox1.Image = new Bitmap(openFileDialog.FileName);
   }
   // store file path in some field or textbox...
   textBox1.Text = openFileDialog.FileName;
}
解决方案1:BLOB方法

// Write to database like this - image is LONGBLOB type
string sql = "INSERT INTO imagetable (image) VALUES (@file)";
// remember 'using' statements to efficiently release unmanaged resources
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        // parameterize query to safeguard against sql injection attacks, etc. 
        cmd.Parameters.AddWithValue("@file", File.ReadAllBytes(textBox1.Text));
        cmd.ExecuteNonQuery();
    }
}

// read image from database like this
string sql = "SELECT image FROM imagetable WHERE ID = @ID";
using (var conn = new MySqlConnection(cs))
{
   conn.Open();
   using (var cmd = new MySqlCommand(sql, conn))
   {
      cmd.Parameters.AddWithValue("@ID", myInt);
      byte[] bytes = (byte[])cmd.ExecuteScalar();   
      using (var byteStream = new MemoryStream(bytes))
      {
         pictureBox1.Image = new Bitmap(byteStream);
      }
   }
}
解决方案2:在文件系统上存储文件路径

// Some file movement to the desired project folder
string fileName = Path.GetFileName(this.textBox1.Text);
string projectFilePath = Path.Combine(projectDir, fileName);
File.Copy(this.textBox1.Text, projectFilePath);

// Write to database like this - imagepath is VARCHAR type
string sql = "INSERT INTO imagepathtable (imagepath) VALUES (@filepath)";
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@filepath", projectFilePath);
        cmd.ExecuteNonQuery();
    }
}

// read from database like this
string sql = "SELECT imagepath FROM imagepathtable WHERE ID = @ID";
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@ID", myInt);
        pictureBox1.Image = new Bitmap(cmd.ExecuteScalar().ToString());
    }
}

我将提供两种解决方案。第一种解决方案是将原始图像以字节为单位直接存储在数据库中。第二种解决方案是我个人推荐的,即改为使用数据库中图像文件的路径

下面是一篇文章的摘录,它提出了一些关于是否BLOB的优秀观点

好吧,有几个原因不应该存储二进制数据 在数据库中:

  • 在SQL数据库中存储数据的全部意义在于对数据进行某种排序和结构,并能够 搜索这些数据。但是你如何搜索一个数据库的二进制数据呢 照片

  • 对于大型数据集,存储二进制数据将迅速增大数据库文件的大小,从而使控制数据大小变得更加困难 数据库

  • 为了在数据库中存储二进制数据,您必须不断地转义和取消转义数据,以确保没有任何中断

  • 在文件系统中存储图像的检索速度略快。现在,以下是您应该这样做的一些原因:

有一个很好的理由可以解释为什么您需要存储二进制数据 在数据库中:

  • 复制。将图像存储在数据库中可以将所有数据集中存储,这样更便于携带和使用 复制
以下是如何选择图像文件:

using (var openFileDialog = new OpenFileDialog())
{
   openFileDialog.Title = "Choose Image File";
   openFileDialog.InitialDirectory =
                Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
   openFileDialog.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
   openFileDialog.Multiselect = false;
   if (openFileDialog.ShowDialog() == DialogResult.OK)
   {
       pictureBox1.Image = new Bitmap(openFileDialog.FileName);
   }
   // store file path in some field or textbox...
   textBox1.Text = openFileDialog.FileName;
}
解决方案1:BLOB方法

// Write to database like this - image is LONGBLOB type
string sql = "INSERT INTO imagetable (image) VALUES (@file)";
// remember 'using' statements to efficiently release unmanaged resources
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        // parameterize query to safeguard against sql injection attacks, etc. 
        cmd.Parameters.AddWithValue("@file", File.ReadAllBytes(textBox1.Text));
        cmd.ExecuteNonQuery();
    }
}

// read image from database like this
string sql = "SELECT image FROM imagetable WHERE ID = @ID";
using (var conn = new MySqlConnection(cs))
{
   conn.Open();
   using (var cmd = new MySqlCommand(sql, conn))
   {
      cmd.Parameters.AddWithValue("@ID", myInt);
      byte[] bytes = (byte[])cmd.ExecuteScalar();   
      using (var byteStream = new MemoryStream(bytes))
      {
         pictureBox1.Image = new Bitmap(byteStream);
      }
   }
}
解决方案2:在文件系统上存储文件路径

// Some file movement to the desired project folder
string fileName = Path.GetFileName(this.textBox1.Text);
string projectFilePath = Path.Combine(projectDir, fileName);
File.Copy(this.textBox1.Text, projectFilePath);

// Write to database like this - imagepath is VARCHAR type
string sql = "INSERT INTO imagepathtable (imagepath) VALUES (@filepath)";
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@filepath", projectFilePath);
        cmd.ExecuteNonQuery();
    }
}

// read from database like this
string sql = "SELECT imagepath FROM imagepathtable WHERE ID = @ID";
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@ID", myInt);
        pictureBox1.Image = new Bitmap(cmd.ExecuteScalar().ToString());
    }
}

我将提供两种解决方案。第一种解决方案是将原始图像以字节为单位直接存储在数据库中。第二种解决方案是我个人推荐的,即改为使用数据库中图像文件的路径

下面是一篇文章的摘录,它提出了一些关于是否BLOB的优秀观点

好吧,有几个原因不应该存储二进制数据 在数据库中:

  • 在SQL数据库中存储数据的全部意义在于对数据进行某种排序和结构,并能够 搜索这些数据。但是你如何搜索一个数据库的二进制数据呢 照片

  • 对于大型数据集,存储二进制数据将迅速增大数据库文件的大小,从而使控制数据大小变得更加困难 数据库

  • 为了在数据库中存储二进制数据,您必须不断地转义和取消转义数据,以确保没有任何中断

  • 在文件系统中存储图像的检索速度略快。现在,以下是您应该这样做的一些原因:

有一个很好的理由可以解释为什么您需要存储二进制数据 在数据库中:

  • 复制。将图像存储在数据库中可以将所有数据集中存储,这样更便于携带和使用 复制
以下是如何选择图像文件:

using (var openFileDialog = new OpenFileDialog())
{
   openFileDialog.Title = "Choose Image File";
   openFileDialog.InitialDirectory =
                Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
   openFileDialog.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
   openFileDialog.Multiselect = false;
   if (openFileDialog.ShowDialog() == DialogResult.OK)
   {
       pictureBox1.Image = new Bitmap(openFileDialog.FileName);
   }
   // store file path in some field or textbox...
   textBox1.Text = openFileDialog.FileName;
}
解决方案1:BLOB方法

// Write to database like this - image is LONGBLOB type
string sql = "INSERT INTO imagetable (image) VALUES (@file)";
// remember 'using' statements to efficiently release unmanaged resources
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        // parameterize query to safeguard against sql injection attacks, etc. 
        cmd.Parameters.AddWithValue("@file", File.ReadAllBytes(textBox1.Text));
        cmd.ExecuteNonQuery();
    }
}

// read image from database like this
string sql = "SELECT image FROM imagetable WHERE ID = @ID";
using (var conn = new MySqlConnection(cs))
{
   conn.Open();
   using (var cmd = new MySqlCommand(sql, conn))
   {
      cmd.Parameters.AddWithValue("@ID", myInt);
      byte[] bytes = (byte[])cmd.ExecuteScalar();   
      using (var byteStream = new MemoryStream(bytes))
      {
         pictureBox1.Image = new Bitmap(byteStream);
      }
   }
}
解决方案2:在文件系统上存储文件路径

// Some file movement to the desired project folder
string fileName = Path.GetFileName(this.textBox1.Text);
string projectFilePath = Path.Combine(projectDir, fileName);
File.Copy(this.textBox1.Text, projectFilePath);

// Write to database like this - imagepath is VARCHAR type
string sql = "INSERT INTO imagepathtable (imagepath) VALUES (@filepath)";
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@filepath", projectFilePath);
        cmd.ExecuteNonQuery();
    }
}

// read from database like this
string sql = "SELECT imagepath FROM imagepathtable WHERE ID = @ID";
using (var conn = new MySqlConnection(cs))
{
    conn.Open();
    using (var cmd = new MySqlCommand(sql, conn))
    {
        cmd.Parameters.AddWithValue("@ID", myInt);
        pictureBox1.Image = new Bitmap(cmd.ExecuteScalar().ToString());
    }
}

我将提供两种解决方案。第一种解决方案是将原始图像以字节为单位直接存储在数据库中。第二种解决方案是我个人推荐的,即改为使用数据库中图像文件的路径

下面是一篇文章的摘录,它提出了一些关于是否BLOB的优秀观点

好吧,有几个原因不应该存储二进制数据 在数据库中: