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.
Step 2: When you clicked
Microsoft Office you will see option of
Microsoft Access 2010 database, click on it as seen below:
Step 3: Select a
Blank database as in the following image:
Step 4: Make a table in it, and then save it as in the following:
Step 5: I have filled the table data as in the following image:
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.
- using System.Windows.Forms;
- using System.Data;
- using System.Data.OleDb;
-
- namespace WindowsFormsDataGrid
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- private void Form2_Load(object sender, EventArgs e)
- {
- string strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = D:\\Database1.accdb";
- string strSQL = "SELECT * FROM Developer";
-
- OleDbConnection myConn = new OleDbConnection(strDSN);
- OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn);
- myConn.Open();
- DataSet dtSet = new DataSet();
- myCmd.Fill(dtSet, "Developer");
- DataTable dTable = dtSet.Tables[0];
- dataGridView1.DataSource = dtSet.Tables["Developer"].DefaultView;
- myConn.Close();
- }
- }
- }
Output:
To read other articles based on the same
Microsoft Access Database application functionality , please refer to the below links: