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

Submitted by: 
Visitors have accessed this post 156 times.

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

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.