ASP.Net C# access database
ASP.Net C# syntax for Access database
MS Access Database connection string for OLEDB Provider:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("PATH OF MS ACCESS DATABASE FILE");
OLEDB (Object Linking and Embedding Database Drivers Provider)
Place your MS Access Database file inside the default App_Data Folder of ASP.Net 2.0. Then you can use the Server.MapPath("App_Data/db.mdb") to provide the file path of access database. You can host your database directly into App_Data folder while you are hosting your ASP.Net 2.0 web application on the web server.
Import Namespaces for Oledb
using System.Data; using System.Data.OleDb;
See the C# Source code below for ASP.Net 2.0 connection to access database:
// Access Database oledb connection string
// Using Provider Microsoft.Jet.OLEDB.4.0
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../App_Data/db1.mdb");
// Object created for Oledb Connection
OleDbConnection myAccessConnection = newOleDbConnection(connStr);
// If condition that can be used to check the access database connection
// whether it is already open or not.
if (myAccessConnection.State == ConnectionState.Closed)
{
myAccessConnection.Open();
}
// Message that displays the access database connection state
Response.Write("ASP.Net Access Database Connection State: <b>" + myAccessConnection.State + "</b>");
// If condition to check the access database connection state
// If it is open then close it.
if (myAccessConnection.State == ConnectionState.Open)
{
myAccessConnection.Close();
}
Continue to next tutorial: ASP.Net Connect to SQL Database to learn how to connect to SQL database using VB code.
