C# Exercises

Home AgriMetSoft About Contact

Read Insert Delete Records in MS-Access File C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Insert_Delete_Data_in_Access
  public partial class Form1 : Form
    OleDbConnection OledbConn;
    OleDbDataAdapter adapter;
    string connString = "";
    DataSet ds;
    // https://www.microsoft.com/en-us/download/details.aspx?id=13255
    public Form1()

    private void Select_Access_Click(object sender, EventArgs e)
      OpenFileDialog opf = new OpenFileDialog();
      opf.Title = "Open Excel File.";
      opf.Filter = "Access File(*.accdb)|*.accdb";
      var result = opf.ShowDialog();
      if (result == DialogResult.OK)
        System.IO.FileInfo finfo = new System.IO.FileInfo(opf.FileName);
        connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + opf.FileName;
        OledbConn = new OleDbConnection();
        string sql = "SELECT * FROM Table1";
        OledbConn.ConnectionString = connString;
        ds = new DataSet();
        BindingSource bSource = new BindingSource();
        adapter = new OleDbDataAdapter(sql, OledbConn);
        dataGridView1.DataSource = ds.Tables[0];
        insert.Enabled = true;
        textBox1.Enabled = true;
        textBox2.Enabled = true;

    private void insert_Click(object sender, EventArgs e)
        OleDbCommand insertCommand = new OleDbCommand("INSERT INTO TABLE1 ([First Name],[Last Name]) VALUES (?,?)", OledbConn);
        insertCommand.Parameters.AddWithValue("@First Name", textBox1.Text);
        insertCommand.Parameters.AddWithValue("@Last Name", textBox2.Text);
        MessageBox.Show("Data is Inserted");
      catch (Exception ex)

    private void refresh_Click(object sender, EventArgs e)
      ds = new DataSet();
      dataGridView1.DataSource = null;
      dataGridView1.DataSource = ds.Tables[0];

    private void delete_Click(object sender, EventArgs e)
      OleDbCommand deleteCommand = new OleDbCommand("DELETE FROM TABLE1 WHERE [ID] = ?", OledbConn);
      deleteCommand.Parameters.AddWithValue("@ID", Convert.ToInt16(textBox3.Text));
      MessageBox.Show("The row is deleted!");


Download the project of Visual Studio 2013 in DropBox Download

How to Read Insert Delete Data in MS Access DataBase C# .Net

List of Exercises