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


- 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......
Add your comment

If you have any comments to make about this article, please use this form to do so. Make sure that your comment relates specifically to the article above. More general comments can be posted through the form on the Contact page.

Please note, all comments are moderated, and I end up deleting quite a lot. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Requests to fix your code (post a question to instead, please)
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam

I do not pass email addresses on to spammers, so a valid one will assist me in responding to you personally if required.

Recent Comments

hosein ey 2/25/2015 1:56 PM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
tnx for this article do you think next version of entityframework support's the sql server and ?...

Saywer Ford 2/25/2015 5:15 AM
In response to Optimising ASP.NET Web Pages Sites - Bundling And Minification
Great Article. I did everything right and working fine. How about page specific js files. Lets I...

Saravanan 2/24/2015 10:54 AM
In response to Optimising ASP.NET Web Pages Sites - Bundling And Minification
Hi, Great article about the Bundling and minification. Regards the caching as you mentioned the...

Justin 2/24/2015 10:43 AM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
I'm having the same problem as Ingmar. Everything is working fine locally in Visual Studio, but I it...

Logan Mudlo 2/23/2015 4:59 PM
In response to WebMatrix - A First Application
Is there a way to prevent the automatic close on a Database.Open() call?...

Mog0 2/23/2015 11:16 AM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Probably a silly question but why did you disable cascade delete and implement it yourself?...

Jose-Maria 2/19/2015 3:29 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Views and Controllers
Just great ! Many thanks Mike....

Satyabrata Mohapatra 2/19/2015 12:02 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Views and Controllers
This is great!!...

Ryan Helmoski 2/19/2015 10:24 AM
In response to Conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
Thank you!...

Harsha 2/19/2015 8:27 AM
In response to iTextSharp - Adding Text with Chunks, Phrases and Paragraphs
Hi, Can text area be created so that I can type text in the area in pdf document? We have Add text...