views:

2104

answers:

1

Hello, 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
        }
}
+1  A: 

"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.

AaronLS
Oh, so I have to include the file itself! Thank you very much for the help.
Xesaniel
If it is a *.accdb file then they cannot deny privileges to a group/user because user level security has been removed from the engine.
onedaywhen
Jet ULS never had DENY permission, so it's pretty clear that aaronis was referring to NTFS security.
David-W-Fenton
@David W. Fenton: I think you are right, perhap it only had REVOKE. When the Access team removed user level security from the engine I deleted my test environment so I can't test (not prepared to recreate it, more like!)
onedaywhen