Windows Forms: How to Connect to MS Access Database in C#

This post shows you How to Connect to Microsoft Access Database in C# .NET Windows Forms Application.

To play the demo, you need to create a sample Microsoft Access Database, then create a new Windows Forms Application project.

server explorer in visual studio

To connect to the access database in Visual Studio, you need to select Server Explorer, then select the connection icon to connect to the database.

add connection in visual studio

Clicking the Change button, then select change your data source to Microsoft Access Database file.

change data source in visual studio

Click OK to continue

add connection in visual studio

Next, you need to select the Microsoft Access Database file you want to connect.

Finally, you need to check the connection, if the connection fails, you need to install the microsoft access engine, then close and reopen your visual studio to reconnect the access database.

test connection succeeded

Note, To fix Unrecognized database format '*..accdb' for Microsoft Access Database file or Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine you should install the Microsoft Access Engine, try to 32 bit. If you can't install 32 bit you can remove the Microsoft Office, then install Microsoft Access Engine 32 bit and reinstall the Microsoft Office 32 bit or 64 bit that you want.

visual studio server explorer

After connecting to the access database successfully, you will see your database on the left, under server explorer.

create dataset in visual studio

You need to create a DataSet, then drag the tables that you want to connect from the access database into your DataSet.

Next, Open your form designer, then drag the DataGridView control from the visual studio toolbox to your winform.

Finally, you need to add a DataSource to your DataGridView. As you can see, Visual Studio automatically creates bindingSource, which helps connect data between the application and the access database.

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'appData.Contact' table. You can move, or remove it, as needed.
    this.contactTableAdapter.Fill(this.appData.Contact);

}

Now open the app.config file and you'll see the connection string is automatically added to the configuration file

<connectionStrings>
  <add name="CSAccessDatabase.Properties.Settings.CustomerDatabaseConnectionString"
      connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\CustomerDatabase.accdb"
      providerName="System.Data.OleDb" />
</connectionStrings>

Through this article, I have shown you how to connect the acccess database from visual studio and windows forms application using c # programming language.

In addition, I have shown you how to fix errors connecting to an access database. Such as, 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine or how to fix Microsoft Access Unrecognized Database Format Error.

VIDEO TUTORIAL

Related