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.

You might also like...

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


- 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

Pam 30/08/2017 11:30
In response to Sending Email in Razor Pages
Mike, RazorPages sound like a nice choice for somebody still working in ASP classic who wants to to...

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...