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.

Provider

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:


<asp:AccessDataSource 
        ID="AccessDataSource1" 
        runat="server" 
        DataFile="~/App_Data/MyDb.mdb" 
        SelectCommand="Select * From MyTable">
    </asp:AccessDataSource>

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" 
    runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    ProviderName="<%$ ConnectionStrings:MyConnectionString.ProviderName %>"
    SelectCommand="SELECT * FROM MyTable">
</asp:SqlDataSource>

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:


<connectionStrings>
    <add name="MyConnectionString"
    connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
           Data Source=&quot;D:\Web Projects\MyProject\App_Data\My.mdb&quot;"
        providerName="System.Data.OleDb" />
</connectionStrings>

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


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

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: Saturday, May 17, 2008 9:48 AM
Last Updated: Sunday, June 13, 2010 10:20 AM
Posted by: Mikesdotnetting
Total Views to date: 82136

13 Comments

Thursday, February 5, 2009 11:46 PM - 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.

Friday, February 6, 2009 8:56 AM - Mike

@Bob

I wouldn't recommend the use of ODBC for Access: http://support.microsoft.com/kb/299973/. 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 http://www.connectionstrings.com is where you shoud start looking.

Good luck!

Tuesday, April 28, 2009 8:46 AM - Alka Singh

Hi,

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'.

Tuesday, June 23, 2009 5:23 PM - programmer

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

Thursday, May 13, 2010 9:48 AM - 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?

Thursday, May 13, 2010 7:14 PM - Mike

@mj

Not necessarily. ACE will read 2003 databases.

Sunday, June 6, 2010 2:45 PM - Steve Wellens

That is great information and advice.

Thank You.

Friday, March 25, 2011 2:04 AM - Ryan

I learned alot from this. Thank you so much!

Wednesday, December 12, 2012 11:39 AM - Raju

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

Sunday, March 10, 2013 10:22 AM - indu

Hi,

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.

Tuesday, August 20, 2013 9:28 PM - 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?

Regards,

Geert Kamphuis

Thursday, August 29, 2013 9:59 AM - Mike

@Geert

I have no idea.

Tuesday, September 17, 2013 2:30 PM - Sam

I want co connect the .accdb database in my asp.net 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 some may not be published. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Not relevant to the article
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam
  • Anything in a language I don't understand including gibberish.

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