Read Microsoft Access Database In C# 6

This article is based on the original article that was previously written using an 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 the 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.   public partial class Form2: Form {  
  7.     public Form2() {  
  8.       InitializeComponent();  
  9.     }  
  10.   
  11.     private void Form2_Load(object sender, EventArgs e) {  
  12.       string strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = D:\\Database1.accdb";  
  13.       string strSQL = "SELECT * FROM Developer";  
  14.       // create Objects of ADOConnection and ADOCommand    
  15.       OleDbConnection myConn = new OleDbConnection(strDSN);  
  16.       OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn);  
  17.       myConn.Open();  
  18.       DataSet dtSet = new DataSet();  
  19.       myCmd.Fill(dtSet, "Developer");  
  20.       DataTable dTable = dtSet.Tables[0];  
  21.       dataGridView1.DataSource = dtSet.Tables["Developer"].DefaultView;  
  22.       myConn.Close();  
  23.     }  
  24.   }  
Output:
 
output
 
To read other articles based on the same Microsoft Access Database application functionality, please refer to the below links:


Similar Articles