The connection string for MS Access 2007 database can also be stored inside the web.config file to connect it to the ASP.Net web page using C#. In MS Access 2007 the Microsoft has introduced a new database file type that is .accdb extension. The internal features of the MS Access 2007 can be performed normally like in the previous versions. You can create the MS Access Database and save it into the App_Data directory of the ASP.Net web application. The App_Data directory is also a new feature of ASP.Net 2.0 framework that enables you to store the data items inside it as secure items that cannot be accessed using http request openly. The MS Access 2007 database can be connected using the OleDb data provider of ASP.Net framework that enables you to connect and perform the data operations using C# code easily.
<connectionStrings>
<add name="AccessConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db1.accdb;Persist Security Info=False;Jet OLEDB:Database Password=;" providerName="System.Data.OleDb" />
</connectionStrings>
C# Code for Connecting the Web Page to MS Access 2007 Database File
Namespace Required
using System.Configuration;
using System.Data;
using System.Data.OleDb;
// create an OldDbConnection object and initialize it by passing connection string.
// ConfigurationManager class provides the ConnectionStrings collection type property
// to access the connection string stored in the web.config file.
OleDbConnection myDataConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString);
// open the data connection.
myDataConnection.Open();
// display the connection's current state open/closed.
Response.Write(string.Format("<i><b>Connection state:</b> {0}</i><br />", myDataConnection.State));
// close the data connection.
myDataConnection.Close();
// again display the connection's current state open/closed.
Response.Write(string.Format("<i><b>Connection state:</b> {0}</i>", myDataConnection.State));
// dispose the connection object to release the resources.
myDataConnection.Dispose();