Inserting Image in Microsoft Access Database Using C#

In this tutorial, I will teach you how to insert an image in MS Access Database Using C#. This method has the ability to store an image into Microsoft Access database. I used an OleDB object in the data types of a field in order to save an image itself. Let's begin.

Creating an Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#. figure 1

Step 2

Do the form just like shown below. figure 2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB to access OLeDB libraries .
  1. Using System.Data.OleDb

Step 4

Create a connection between the MS access database and c#. After that, declare all the classes and variables that are needed.
  1.  
  2. OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\dbsaveimg.accdb");
  3. OleDbCommand cmd = new OleDbCommand();
  4. String sql;
  5. OpenFileDialog ofd = new OpenFileDialog();

Step 5

Create a method for Saving an image in the database.
  1.  
  2. private void saveImage(string sql)
  3. {
  4. try
  5. {
  6. con.Open();
  7. string path = ofd.FileName;
  8. byte[] imageData;
  9.  
  10. cmd = new OleDbCommand();
  11.  
  12. cmd.Connection = con;
  13. cmd.CommandText = sql;
  14. imageData = System.IO.File.ReadAllBytes(@path);
  15. cmd.Parameters.AddWithValue("@IM", imageData);
  16. cmd.ExecuteNonQuery();
  17.  
  18. }
  19. catch(Exception ex)
  20. {
  21. MessageBox.Show(ex.Message);
  22. }
  23. finally
  24. {
  25. con.Close();
  26. }
  27. }
Step 6 Write the following code to execute the saveImage() method when the “Save” button is clicked.
  1.  
  2. private void btnSave_Click(object sender, EventArgs e)
  3. {
  4. sql = "Insert into tblimage (img) Values (@IM)";
  5. saveImage(sql);
  6. MessageBox.Show("Image has been saved into the database");
  7. }
Step 7 Write the following code to get an image in the file directory when the Browse button is clicked.
  1.  
  2. private void btnBrowse_Click(object sender, EventArgs e)
  3. {
  4.  
  5. //'CHECK THE SELECTED FILE IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  6. ofd.CheckFileExists = true;
  7.  
  8. //'CHECK THE SELECTED PATH IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  9. ofd.CheckPathExists = true;
  10.  
  11. //'GET AND SET THE DEFAULT EXTENSION
  12. ofd.DefaultExt = "jpg";
  13.  
  14. //'RETURN THE FILE LINKED TO THE LNK FILE
  15. ofd.DereferenceLinks = true;
  16.  
  17. //'SET THE FILE NAME TO EMPTY
  18. ofd.FileName = ".jpg";
  19.  
  20. //'FILTERING THE FILES
  21. ofd.Filter = "(*.jpg)|*.jpg|(*.png)|*.png|(*.jpg)|*.jpg|All files|*.*";
  22. //'SET THIS FOR ONE FILE SELECTION ONLY.
  23. ofd.Multiselect = false;
  24.  
  25. //'SET THIS TO PUT THE CURRENT FOLDER BACK TO WHERE IT HAS STARTED.
  26. ofd.RestoreDirectory = true;
  27.  
  28. //'SET THE TITLE OF THE DIALOG BOX.
  29. ofd.Title = "Select a file to open";
  30.  
  31. //'ACCEPT ONLY THE VALID WIN32 FILE NAMES.
  32. ofd.ValidateNames = true;
  33.  
  34. if (ofd.ShowDialog() == DialogResult.OK)
  35. {
  36. PictureBox1.Image = Image.FromFile(ofd.FileName);
  37. }
  38. }
Download the complete source code and run it on your computer. For any questions about this article. You can contact me @ Email – jannopalacios@gmail.com Mobile No. – 09305235027 – TNT Or feel free to comment below

Add new comment