Connecting to a Microsoft Access database with ASP.NET - Connecting to a password protected Microsoft Access database directly using ASP.NET
(Page 6 of 6 )
For this demonstration, I created another database named “MyDBwithPwd.mdb.” It was protected with the password “admin.” Now, we need to open a password protected Access database using ASP.NET.
Try to create your own web application using Visual Studio.NET (I named mine “ConnectAccessWithPassword” for this demonstration). Design your web form with the same layout as shown in the first section of this article.
After designing the form, switch to the code and add the following line at the top.
Imports System.Data.OleDb
Add the following code to your “show/refresh” button:
Private Sub btnList_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnList.Click
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\AccessDB\MyDBwithPwd.mdb;User Id=admin;Jet
OLEDB:Database Password=admin")
Dim da As New OleDbDataAdapter("select * from emp", cn)
Dim dt As New DataTable
da.Fill(dt)
da.Dispose()
cn.Dispose()
Me.DataGrid1.DataSource = dt
Me.DataGrid1.DataBind()
End Sub
The most important specification from the above code is the following line:
Dim cn As New OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\AccessDB\MyDBwithPwd.mdb;User Id=admin;Jet
OLEDB:Database Password=admin")
I specified the password by adding “Jet OLEDB:Database Password=admin” to the connection string. Don’t forget to specify “impersonation” in the “web.config” file when you want to manipulate the data.
Sometimes, you may receive another error: “The Microsoft Jet database engine cannot open the file 'C:\Inetpub\wwwroot\WebAppSample\SampleDB.mdb'. It is already opened exclusively by another user, or you need permission to view its data.” The error really makes a programmer’s life miserable.
That error generally occurs due to lack of permissions. Try to give all rights (Read, Write, Modify etc.) to the folder having the Access database. The best way to keep away from that error is to just keep the database out of any virtual directory and follow the rules according to the first two sections. It should be perfectly all right.
If you are storing an Access database on network storage (especially shared folders on a network), you still need to provide proper permissions to connect and modify the database information.
Any comments, suggestions, feedback, bugs, errors, enhancements are highly appreciated at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |