In this tutorial, I'll show you how to connect to microsoft access database in windows forms application using c# code. I'll create a customer table then save it with dbcustomer name.

c# connect to access database

As you know, Microsoft access is a database management system from Microsoft that combines the relational Microsoft Jet Database Engine with a graphical user interface and software development tools. You can also use it to import or link directly to data stored in other applications and databases.

We will design a simple UI that allows us to click the button to connect to our database as below

connect to access database in c#

Add your code to handle the connection to the database as below.

private void button1_Click(object sender, EventArgs e)
{
    OleDbConnection cn = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={Application.StartupPath}/dbcustomer.accdb");
    if (cn.State == ConnectionState.Closed)
    {
        cn.Open();
        MessageBox.Show("Connected successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

You should install the Microsoft Access Database Engine 2010 version 64bit or 32bit based on your OS installed. If you get an error 'The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.' you should set the target framework for your project to 64 bit or 32 bit based on the Microsoft Access Engine installed in your computer.

To set the target framework, you can right click on your project->properties->build->64bit