ASP.Net Connect To Access Database

Updated on 03 Apr 2012,
Published on 12 Feb 2008

ASP.Net 2.0 connection string to connect to Ms Access Database:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("PATH OF MS ACCESS DATABASE FILE")

OLEDB provides the drivers to connect to access database.

By default if you will enter the name of database file in Data Source field, then your web application will search the Ms Access Database on C:\ of the web server. That is why you have to place the access database connection string in connection class of your web application instead of web.config file.

Import Namespaces for Oledb

Imports System.Data
Imports System.Data.OleDb 

See the source code below for ASP.Net 2.0 connection to access database using VB code:

' Access Database oledb connection string
' Using Provider Microsoft.Jet.OLEDB.4.0
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("../App_Data/db1.mdb")

' Object created for Oledb Connection
Dim myAccessConnection As 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 Then
    myAccessConnection.Open()
End If

' Message that displays the access database connection state
' 1 for open
' 0 for closed
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 Then
    myAccessConnection.Close()
End If

Continue to next tutorial: ASP.Net Access Database Connection to learn how to connect MS Access database using data source name.

0 Responses to "ASP.Net Connect To Access Database"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers