In this article we will create a Web form that retrieves and displays schema information from an Access database.
The first step to establish a connection to the Access database. In this case I am using xtreme.mdb, a sample database with the Visual Studio .NET 2003 installation. (This database is included in the code sample download.)
Figure 1: Establish a Connection to the Access Database
// Estblish connection string
public static readonly string STRCONN = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=D:\\Dot Net\\ASPAlliance\\Article631\\xtreme.mdb;";
// Establish the OleDb connection to your access database
OleDbConnection oleDbConn = new OleDbConnection(STRCONN);
If you do not like the idea of storing the database connection string within your code, you can instead retrieve it from the Web.config configuration file.
Figure 2: Store Database Connection String within Web.config
<configuration>
<appSetting>
<add key=”ConnectionString” value=” Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=D:\\Dot Net\\ASPAlliance\\Article631\\xtreme.mdb;” />
</appSetting>
</configuration>
We use the following code to retrieve the database connection string from the Web.config file:
Figure 3: Retrieve the Database Connection String
using System.Configuration;
string strConn = (string)ConfigurationSettings.AppSettings[“ConnectionString”];