Skip to main content

Clever Way to Save Multiple Data Using C# and MS Access Database

Now, in this tutorial, I’m going to teach you how to save multiple data in C# and MS Access Database. There are times that you are having a hard time on what to do with saving multiple data. Well, this tutorial is just right for you. This is just simple yet very helpful when you happened to encounter this kind of problem. All you have to do is follow all the instructions that are shown below.

Creating an Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in C#.

Step 2

Do the form just like shown below.

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

Establish a connection between the ms access database and c#. After that, declare a class and variable that is needed.

  1. OleDbConnection con = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + Application.StartupPath + "/studentdb.accdb");
  2. OleDbCommand cmd;
  3. string sql;

Step 5

Create a method for saving data.

  2. private void Save_Data(string sql)
  3. {
  4. try
  5. {
  6. con.Open();
  8. cmd = new OleDbCommand();
  10. cmd.Connection = con;
  11. cmd.CommandText = sql;
  12. cmd.ExecuteNonQuery();
  14. }
  15. catch (Exception ex)
  16. {
  17. MessageBox.Show(ex.Message);
  18. }
  19. finally
  20. {
  21. con.Close();
  22. }
  23. }

Step 6

Do the following codes to execute the method that you have created in saving multiple data when the button is clicked.

  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. for(int i = 0;i < dataGridView1.Rows.Count - 1; i++)
  5. {
  6. sql = "INSERT INTO tblstudent (fname,lname) VALUES ('" + dataGridView1.Rows[i].Cells[0].Value + "','" + dataGridView1.Rows[i].Cells[1].Value + "')";
  7. Save_Data(sql);
  8. }
  9. MessageBox.Show("Data has been saved in the database.");
  10. }

Download the complete source code and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.