AccessDataSource, SqlDataSource and connecting to Access databases in ASP.NET

There are a variety of options for connecting to Access databases within ASP.NET pages. This article attempts to cover the choices and offer recommendations for getting the best out of Access.


There are two providers that you can use, depending on the version of Access that you choose. In the vast majority of cases, the .mdb format is used, which equates to Access 2000-2003, and the appropriate provider is the Jet 4.0 OleDb provider. If you are deploying a .accdb file, you are using Access 2007, and this has its own provider - the ACE 12.0 OleDb provider. At the time of writing, the ACE provider is only available as part of Microsoft Office Professional or higher, or as a separate download from Microsoft Downloads. It is not installed on any machine by default. As a result, it is extremely unlikely that the ACE provider will be available on any shared hosting web server. From a web application perspective, the differences between a .mdb file and a .accdb file are so insignificant that I would always recommend that the .mdb format is used. Jet is installed as part of the Windows Operating System on XP, Server 2000, 2003, 2008 and Vista, so should always be available without any manual intervention.

Location of file

For ASP.NET 2.0 or higher, the database file (.mdb or .accdb) should always go into the App_Data folder. There are two reasons for this: first, App_Data is configured to prevent users from browsing to the folder and downloading a copy of your database. Second, you can take advantage of the special |DataDirectory| token (or substitution string) to reference the file within a connection string. |DataDirectory| defaults to the App_Data directory.

AccessDataSource control

The more I consider it, the more pointless this control seems to me. The only good thing about it is that it works. However, it only works for the .mdb format, as it invokes the Jet Provider internally, and this cannot be changed. You cannot use it with password protected file, as it automatically generates the Connection string from the location of the .mdb file, which you feed to the DataFile property. Besides that, it is extremely easy to use. Assuming that you followed advice, and placed your .mdb file in the App_Data folder, you only need to set a DataFile property to reference it:

        SelectCommand="Select * From MyTable">

SqlDataSource control

The SqlDataSource control is a lot more flexible than the AccessDataSource control. Despite its name, it is used to connect to any relational database for which there is an ADO.NET provider. This includes OleDb, ODBC, OracleClient, SqlClient, MySqlClient etc. The SqlDataSource control requires a valid connection string to be provided for the ConnectionString property. In the case of a .mdb file, this is very straightforward - especially for a file placed in App_Data as recommended:

"Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|myDB.mdb"

For a .accdb file, all that needs to be changed is the provider:

"Provider=Microsoft.ACE.OleDb.12.0;Data Source=|DataDirectory|myDB.accdb"

Configuring the SqlDataSource control for Access is a breeze. Once you have selected New Connection, change the Data Source to Microsoft Access Database File, click OK and then browse to the App_Data folder of the project. Select the database and click OK, and you are done. You may like to store the conection string in the web.config file. There are good reasons for doing so, for example, if you ever need to change it, you only have to do so in one place. For this reason, when configuring the SqlDataSource, I recommend choosing the option to save the connection string in the application configuration file.

<asp:SqlDataSource ID="SqlDataSource1" 
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    ProviderName="<%$ ConnectionStrings:MyConnectionString.ProviderName %>"
    SelectCommand="SELECT * FROM MyTable">

You will notice that the main difference between the SqlDataSource and the AccessDataSource is the ConnectionString and ProviderName properties. These reference the values in the web.config <connectionStrings> section. The <connectionStrings> section is a child of the <configuration> node, and when it has been auto-generated by the SqlDataSource control, may look something like this:

    <add name="MyConnectionString"
           Data Source=&quot;D:\Web Projects\MyProject\App_Data\My.mdb&quot;"
        providerName="System.Data.OleDb" />

So the first thing I recommend is making a modification to the Data Source property by getting rid of the &quot; and using |DataDirectory|:

    <add name="MyConnectionString"
        connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|My.mdb"
        providerName="System.Data.OleDb" />

Now that |DataDirectory| is being used, the web site can be transferred to any machine and should run, because the connection string is no longer tied to the file system of the actual machine that the application is developed on.

Date Posted:
Last Updated:
Posted by:
Total Views to date: 92005


- bob moyers

do you have an example of how to connect to an Access database via ODBC from .Net? My web page is on an internet server with Access being on a client intranet server that has an ODBC name setup for access and port 1433 open.

- Mike


I wouldn't recommend the use of ODBC for Access: And Access is definitely not designed for use on a remote server. If the Access database is also being used as the backend to a networked Access Application, you are most likely to suffer performance problems. However, you may have no choice, in which case is where you shoud start looking.

Good luck!

- Alka Singh


i am using SqlDataSource to show data in gridview, and udating data from the grid view. this program is running properly on local but when i am uploading on server then it shows following error :-

System.Data.OleDb.OleDbException: Could not find file 'c:\windows\system32\inetsrv\4saleorlet.mdb'.

- programmer

Very nice writeup. Agree with you on all points and thanks for the help

- mj

hei mike..
if i create a table in access 2007 and save it in access 2003 format.. i should use "Microsoft.Jet.OLEDB.4.0"..
am i right?

- Mike


Not necessarily. ACE will read 2003 databases.

- Steve Wellens

That is great information and advice.

Thank You.

- Ryan

I learned alot from this. Thank you so much!

- Raju

The above explanation is so good meanwhile I need explanation with diagram

- indu


Thanks a lot for this tutorial. I was scratching my head with regards to connection string in webconfig file for Access db and tried many things but in vain. The code |DataDirectory|my.mdb in webconfig file resolved the issue.

Your explanation has been wonderful, and I have marked your site to my favourites.

Thanks again.

- Geert Kamphuis

I'm working with Webmatrix 3.
My web.config contains:
providerName="System.Data.OleDb" />

Both databases are accessible as defined.

But: the connectionstring for OleDb is the reason that the defined sdf-databases are not visible anymore in the Webmatrix databases workspace.
As soon as the OleDb connectionstring is removed, the sdf-databases are again visible the workspace.

Is this a known bug in Webmatrix?


Geert Kamphuis

- Mike


I have no idea.

- Sam

I want co connect the .accdb database in my project.
how to add it , Please help me......

Recent Comments

sandeep 8/28/2015 7:03 AM
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
very good article, i like it........keep writing such quality article in future. thx Mike....

Hassan, MVC Learner 8/28/2015 6:37 AM
In response to Get The Drop On ASP.NET MVC DropDownLists
Great Help, simple, great and patiently explained article !...

Anvesh 8/28/2015 12:39 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
what if we are taking postback values from FormCollections instead of an array int[] category. How...

Ben 8/27/2015 10:50 PM
In response to Simple File Download Protection with ASP.NET
Is it possible to setup your project to publish files to outside of your root directory? I would to...

Fred 8/26/2015 12:50 AM
In response to WebMatrix Opens Wrong Version Of Visual Studio
I enjoyed many of your tutorials but the problem is none of the tutorials are combined like most be....

Muhammad Ashikuzzaman 8/25/2015 2:48 PM
In response to Managing Checkboxes And Radios In ASP.NET Razor Web Pages
That's a very good tips for razor...

Sergey 8/25/2015 8:32 AM
In response to More Flexible Routing For ASP.NET Web Pages
Hi. How I can set up my site to get urldata from link for default page?

Tony Gray 8/25/2015 6:27 AM
In response to Adding Validation
Hi Mike, Really helpful article and series. Thanks. Small typo in 4th paragraph you have so...

amanda n 8/25/2015 12:38 AM
In response to Solving the Operation Must Use An Updateable Query error
Thank you very much. I'm a uni student and while solving coding problems is usually enjoyable and me...

salman 8/23/2015 9:25 AM
In response to How To Send Email In ASP.NET MVC
thanks ......