Read Microsoft Access Database In C# 6

This article is based on the original article that was previously written using older version of Visual Studio. You can find the original article on the below link:

Steps to create Microsoft Access Database:

Step 1: Click on the Microsoft Office option in your search option or through all apps.

1

 Step 2:
When you clicked Microsoft Office you will see option of Microsoft Access 2010 database, click on it as seen below:

2

Step 3:
Select a Blank database as in the following image:
3

Step 4: Make a table in it, and then save it as in the following:

4

Step 5: I have filled the table data as in the following image:

5

Accessing and working with a Microsoft Access database (.accdb file) is a pretty simple job using C# and ADO.NET.

The OleDb data provider in ADO.NET is used to work with Access and other OleDb supported databases. The OleDb data provider is defined in the System.Data.OleDb namespace. So don't forget to add reference to System.Data and System.Data.OleDb namespaces.
In this sample code, I connect and read a Microsoft Access database called Database1.accdb, which has a table called "Developer".

I display the contents of the table in a Windows dataGridView control.

Create a Windows Form application using Visual Studio 2015 and add a dataGridView control to the Form. After that, write the following code on Form's load event handler. You may want to replace this code with the place where you want to read the database, such as a button click event handler.
  1. using System.Windows.Forms;  
  2. using System.Data;  
  3. using System.Data.OleDb;  
  4.   
  5. namespace WindowsFormsDataGrid  
  6. {  
  7.   public partial class Form2 : Form  
  8.   {  
  9.     public Form2()  
  10.     {  
  11.       InitializeComponent();  
  12.     }  
  13.   
  14.     private void Form2_Load(object sender, EventArgs e)  
  15.     { 
  16.       string strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = D:\\Database1.accdb";  
  17.       string strSQL = "SELECT * FROM Developer";  
  18.       // create Objects of ADOConnection and ADOCommand  
  19.       OleDbConnection myConn = new OleDbConnection(strDSN);  
  20.       OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn);  
  21.       myConn.Open();  
  22.       DataSet dtSet = new DataSet();  
  23.       myCmd.Fill(dtSet, "Developer");  
  24.       DataTable dTable = dtSet.Tables[0];  
  25.       dataGridView1.DataSource = dtSet.Tables["Developer"].DefaultView;  
  26.       myConn.Close();  
  27.     }  
  28.   }  
  29. }  
Output:

output

To read other articles based on the same Microsoft Access Database application functionality , please refer to the below links:

Up Next
    Ebook Download
    View all
    Learn
    View all