How to Connect to MS Access Database in C#
By FoxLearn 7/18/2024 3:36:08 AM 15.93K
You can easily connect to a Microsoft Access database in a C# Windows Forms application using ADO.NET.
How to Connect to Microsoft Access Database in C#
Make sure you have the necessary drivers installed on your machine.
To play the demo, you need to create a sample Microsoft Access Database, then Open Visual Studio and create a new Windows Forms Application project.
You can connect to the access database in Visual Studio by selecting the Server Explorer, then select the connection icon to connect to the database.
Clicking the Change button, then select change your data source to Microsoft Access Database file.
Click OK to continue
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.
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.
After connecting to the access database successfully, you will see your database on the left, under server explorer.
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