How to connect to a MS Access database from an ASP Script
Posted by Arvand Sabetian (Import) on 25 August 2009 09:14 PM
|
|
Please use the following sample code to connect to a MS Access .mdb file: <% E:\path\to\file.mdb is where you've uploaded your mdb file and almost always starts off with E:\HostingSpaces\USERNAME where USERNAME is your username. While the above works for many of our older servers. The following works on our newer servers. You'll need to make sure that your application is running in a Dedicated App Pool (check box under the website inside your control panel) and that MDAC is installed on the server which all of our new servers already have installed (ask our support staff if it doesn't work). <% | |
|
Private Function ReadExcelOLEDB() As DataTable
Try
Dim MyConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & ~//ExcelFile//testing.xls & "; Extended Properties=Excel 8.0;")
Dim MyCommand As New System.Data.OleDb.OleDbDataAdapter("select * from [" & Sheet1 & "]", MyConnection)
Dim objDataTable As New DataTable
MyConnection.Open()
MyCommand.Fill(objDataTable)
MyConnection.Close()
Return objDataTable
Catch ex As Exception
End Try
End Function
ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\path\YourUserName\yoursite.com\wwwroot\yourFolder\yourDatabase.mdb;DefaultDir=;UID=;PWD=;"
Dim ConnectionString, CurrentDatabase
ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};" &_
"DBQ=" & Server.MapPath("data\bitethis.mdb") & ";DefaultDir=;UID=;PWD=;"
Set CurrentDatabase = Server.CreateObject("ADODB.Connection")
CurrentDatabase.ConnectionTimeout = 30
CurrentDatabase.CommandTimeout = 80
CurrentDatabase.Open ConnectionString
Also, my connection string was defined in my web.config file. I went in and changed <add name="ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Ranch.accdb;Persist Security Info=True" providerName="System.Data.OleDb" /> to
<add name="ConnectionString" ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Ranch.accdb;" />
Do I need to change the vb code from
Dim RanchConn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Ranch.accdb")
to
Dim RanchConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Ranch.accdb")
Also, in your code above you have Set Connection = Server.CreateObject("ADODB.Connection") Do I need that instead of the way I have it.
PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydomain.com.sage.arvixe.com\wwwroot\folder\mydatabase.mdb;User Id=admin; Password=;
The \path\username\ part is not included. I tried \HostingSpaces\username\ but that didn't work.
Any help would be appreciated.
Thank you!