ASP.Net C# access database

Updated on 13 Feb 2008, Published on 13 Feb 2008

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 = new OleDbConnection(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();
}

 

 

Product Spotlight

Share it
Rate this article:
Email it:
email

0 Responses to "ASP.Net C# access database"
Leave a Reply

Name   (Required)


Mail   (will not be published) (Required)


Website   (http://www.example.com)




OR Subscribe via Email: