C#Access数据库正在使用或权限失败 [英] C# Access Database In use or Permission Failure

查看:412
本文介绍了C#Access数据库正在使用或权限失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Access 2007和C#学习数据库.到目前为止,这很艰难,但是我已经能够很好地处理问题.不过,我需要做的是查询数据库表Accounts中用户根据其密码获得的金额.我在正在使用的Windows窗体上放置了一个按钮,该按钮将在单击时查询数据库.当我正常运行/单击按钮时,收到以下错误.

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.

基本上,我的问题是:如何设置权限,以便程序可以自由访问我拥有的Access数据库?

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?

我的异常错误:

异常:System.Data.OleDb.OleDbbException:Microsoft Office Access数据库引擎无法打开或写入文件'C:\ Users \ Public'.它已经由另一个用户专门打开,或者您需要权限才能查看和写入其数据.

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.

我的代码:

  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"需要更改为要访问的* .mdb文件的实际路径:

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

"C:\ Users \ Public.mdb"

"C:\Users\Public.mdb"

OR

"C:\ Users \ Public \ Something.mdb"

"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=;

或者它可能是* .accdb文件.如:

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;

请参见 http://www.connectionstrings.com/access-2007 http://www.connectionstrings.com/access

此外,如果在另一个程序(如Access 2007)中打开文件,文件被标记为只读"或安全权限导致您没有读取"或写入"访问权限,则有时会遇到这种问题. .请注意,如果您为用户"之类的组设置了拒绝"权限(在文件系统/NTFS中),则它将覆盖所有其他权限,这样管理员将受到拒绝"权限的影响.

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.

感谢您的评论,并增加了一些澄清.

Thanks for comments, added a little clarification.

这篇关于C#Access数据库正在使用或权限失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆