Introduction

In this article we will first create a table in a MySQL database and then we will use a Windows Forms application to save an image into the database.

Description

Create a table in a MySQL database using MySQL browser as in the following. To store the image in the database, here is the script for the table:

  1. CREATE TABLE `image` (
  2. `name` varchar(100) default NULL,
  3. `type` varchar(100) default NULL,
  4. `desc` varchar(100) default NULL,
  5. `docname` varchar(5000) default NULL,
  6. `docdisc` varchar(500) default NULL
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
Create a new Windows Forms application and arrange controls on the form as in the following:

Save.jpg

Add a reference to the MySql.Data DLL using the Add Reference dialog box and include the following two namespaces:
  1. using MySql.Data.MySqlClient;
  2. using System.IO;
Write the following code in the Click event of PictureBox to select an image to be saved in the database:
  1. //The String used to store the location of the file that is currently loaded in the picture box picFile
  2. String location;
  3. //The String used to store the name of the file that is currently loaded in the picture box picFile
  4. String fileName;
  5. private void Browse_Click(object sender, EventArgs e)
  6. {
  7. openPic.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
  8. //Showing the fileopen dialog box
  9. openPic.ShowDialog();
  10. //showing the image opened in the picturebox
  11. pictureBox1.BackgroundImage = new Bitmap(openPic.FileName);
  12. //storing the location of the pic in variable
  13. location = openPic.FileName;
  14. textBox2.Text = location;
  15. //storing the filename of the pic in variable
  16. fileName = openPic.SafeFileName;
  17. }
Write the following code in the Click event of the btnSave Button:
  1. private void btn_SaveImage_Click(object sender, EventArgs e)
  2. {
  3. MySqlConnection con = new MySqlConnection(ConString);
  4. MySqlCommand cmd;
  5. FileStream fs;
  6. BinaryReader br;
  7. try
  8. {
  9. //Creating a filestream to open the image file
  10. FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read);
  11. //Getting the legth of the fil in bytes
  12. int fileLength = (int)fs.Length;
  13. //creating an array to store the image as bytes
  14. byte[] rawdata = new byte[fileLength];
  15. //using the filestream and converting the image to bits and storing it in
  16. //an array
  17. fs.Read(rawdata, 0, (int)fileLength);
  18. //Creating a new mysql command object which will be used to store the image
  19. MySqlCommand cmd = new MySqlCommand();
  20. //creating sql command
  21. String sql = "insert into doc1 values(@pfno,@depname,@doctype,@docdesc,@docexpdate,@docname,@docdisc)";
  22. //Connection
  23. con = new MySqlConnection();
  24. con.ConnectionString = ConfigurationSettings.AppSettings["constr"];
  25. con.Open();
  26. //Setting the connection of the command
  27. cmd = new MySqlCommand(sql, con);
  28. //setting the sql of the command
  29. //cmd.CommandText = sql;
  30. //Setting up the parameter values to be used when storing the image to a
  31. //table
  32. //cmd.Parameters.AddWithValue("@docsize", rawdata);
  33. cmd.Parameters.AddWithValue("@name", label8.Text);
  34. cmd.Parameters.AddWithValue("@type", comboBox1.Text);
  35. cmd.Parameters.AddWithValue("@desc", textBox1.Text);
  36. cmd.Parameters.AddWithValue("@docname", textBox2.Text);
  37. cmd.Parameters.AddWithValue("@docdisc", fileLength);
  38. //Executing the query and writing the image to the database
  39. cmd.ExecuteNonQuery();
  40. //Closing the filestream
  41. con.Close();
  42. MessageBox.Show("Done");
  43. }
  44. catch (Exception ex)
  45. {
  46. MessageBox.Show(ex.Message);
  47. }
  48. }
The comments are given in // blocks.