In this article, you’ll learn how to connect and read Microsoft Access 2003 or earlier versions (.mdb).
Note: Originally, this article was published on Jan 01, 2000. This is an updated article.
The code snippet in this article is a simple console application that connects with an Access 2000 database, reads data from a table, and displays it on the console. The database has a table named, Developer with two columns, Name and Address.
The .NET framework has two common approaches, ADO.NET and LINQ to read databases. ADO.NET uses the OLE-DB data provider to read a Microsoft Access database. The OLE-DB classes are defined in the System.Data.OleDb namespace. We must import the System.Data.OleDb namespace in our code using the following definition.
- using System.Data.OleDb;
The following code is the complete list of the C# console app that connects with an Access database, opens a connection, reads data, and displays it on the system console.
The following code snippet adds a new row in the table. The INSERT SQL query is used to add a new row.
- using System;
- using System.Data.OleDb;
- namespace AccessDBSample {
- class Program {
- static void Main(string[] args) {
- // Connection string and SQL query
- string connectionString = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Mahesh\Data\Dev.mdb";
- string strSQL = "SELECT * FROM Developer";
- // Create a connection
- using(OleDbConnection connection = new OleDbConnection(connectionString)) {
- // Create a command and set its connection
- OleDbCommand command = new OleDbCommand(strSQL, connection);
- // Open the connection and execute the select command.
- try {
- // Open connecton
- connection.Open();
- // Execute command
- using(OleDbDataReader reader = command.ExecuteReader()) {
- Console.WriteLine("------------Original data----------------");
- while (reader.Read()) {
- Console.WriteLine("{0} {1}", reader["Name"].ToString(), reader["Address"].ToString());
- }
- }
- } catch (Exception ex) {
- Console.WriteLine(ex.Message);
- }
- // The connection is automatically closed becasuse of using block.
- }
- Console.ReadKey();
- }
- }
- }
- // Add a new row
- strSQL = "INSERT INTO Developer(Name, Address) VALUES ('New Developer', 'New Address')";
- command = new OleDbCommand(strSQL, connection);
- // Execute command
- command.ExecuteReader();
The following code snippet updates rows that match with the WHERE condition in the query.
- // Update rows
- strSQL = "UPDATE Developer SET Name = 'Updated Name' WHERE Name = 'New Developer'";
- command = new OleDbCommand(strSQL, connection);
- command.ExecuteReader();
The following code snippet deletes rows that match with the WHERE condition.
- // Delete rows
- strSQL = "DELETE FROM Developer WHERE Name = 'Updated Name'";
- command = new OleDbCommand(strSQL, connection);
- command.ExecuteReader();
Note: Access 2013 or Access 2016 versions work differently. If you want to work with Access 2013, 2016 or Office 365 databases, visit this article: Connecting C# Application To MS Access Database

Guest UserPosted Nov 2, 2020, 6:05 AM
Very informative
Mahesh ChandPosted Apr 15, 2017, 6:37 PM
OK finally, I updated this 18 years old article. Code still works :)
Ammar ShaukatPosted Apr 15, 2016, 2:04 AM
Good . this is was your First article amazing..
jack the bestPosted Feb 12, 2015, 9:53 PM
hi can you help me?how add you category for the products and on product frm show you a combox with all category?
Mahesh ChandPosted Oct 24, 2012, 8:54 PM
O wow! I wrote this article 12 or so years ago. When I just started learning C# and common database was Access.
sailaja mPosted Aug 16, 2011, 7:37 AM
is it possible connect to MS SQL Server from HTML Page using Javascript? Plz help me Thanks in advance
Rashid ChakiPosted Sep 18, 2010, 4:37 PM
Hi Mahesh bhai, I want to make web application to sent bulk email, id is in excel file. Please help me, you are the one can help. Thanks, Rashid Ahmedabad(gujrat) [email protected]
DudePosted Apr 28, 2010, 9:33 PM
Hi, Lets say, the Access database that is used has a User & password property. How should I enter it? Thanx.
patrickPosted Aug 1, 2009, 10:03 PM
I would love to see an updated publish of this article.