C# Access Database In use or Permission Failure – Education Career Blog

I’m using Access 2007 and C# to learn Databases. So far it’s been rough but I’ve been able to handle things relatively well. What I need to do though is to query a my database table Accounts for the Amount of money a user has based on their pin. I’ve placed a button on the Windows Form I am using that will query the database on click. When I run/click the button as per normal I recieve the following error.

Essentially my question is this: How would I go about setting the permissions up so that my program can freely access the Access Database I have?

My Exception Error:

Exception: System.Data.OleDb.OleDbException: The Microsoft Office Access database engine cannot open or write to the file ‘C:\Users\Public’. It is already opened exclusively by another user, or you need permission to view and write its data.

My code:

  public partial class frmPin : Form
{
    static string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public;Persist Security Info=True";
    static private int pin = 11; //The First Pin in my records, for debugging I inserted it directly.
    static string selectStatement = "SELECT Amount FROM Accounts WHERE(PIN=" + pin + ")";
    OleDbConnection conn = new OleDbConnection(connString);
    OleDbCommand cmd = new OleDbCommand(selectStatement);



    public frmPin()
    {
        InitializeComponent();

    }

    private void btnQry_Click(object sender, EventArgs e)
    {
       try
       {
           conn.Open();
           OleDbDataReader reader = cmd.ExecuteReader(); // executes query
           while (reader.Read()) // if can read row from database
            {
                txtBx.Text = reader.GetValue(1).ToString();
            }
        }
        catch (Exception ex)
        {
            txtBx.Text = "Exception: " + ex;  // Displays Exception
        }
        finally
        {
            conn.Close();  // finally closes connection
        }
}   

,

“C:\Users\Public” needs to be changed to the actual path of the *.mdb file you want to access:

“C:\Users\Public.mdb”

OR

“C:\Users\Public\Something.mdb”

Depending on the name of your database:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;

Or it may be an *.accdb file. Such as:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;

See http://www.connectionstrings.com/access-2007 and http://www.connectionstrings.com/access

Also, sometimes you will get this kind of problem if you have the file open in another program like Access 2007, the file is marked as Read Only, or the security permissions are such that you don’t have Read or Write Access. Note that if you set a “Deny” permission (in the filesystem/NTFS) for a group like Users, then it will override all other permissions, such that an Administrator would be effected by the Deny permission.

Edit: Thanks for comments, added a little clarification.

Leave a Comment