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

3.84 (31 votes)

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: 103949


- 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:
<add name="conHC" connectionString="Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|HC.mdb"
providerName="System.Data.OleDb" />

<add name="HCSdf" connectionString="Data Source=|DataDirectory|HuwContr1.sdf"/>

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

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....