我正在使用Access 2007和C#来学习数据库。到目前为止,它一直很粗糙,但我已经能够处理相对较好的事情了。我需要做的是查询我的数据库表帐户中的用户根据其引脚的金额。我在我正在使用的Windows窗体上放置了一个按钮,它将在单击时查询数据库。当我按照正常运行/单击按钮时,我收到以下错误。
基本上我的问题是:如何设置权限以便我的程序可以自由访问我拥有的Access数据库?
我的例外错误:
异常:System.Data.OleDb.OleDbException:Microsoft Office Access数据库引擎无法打开或写入文件“C:\ Users \ Public”。它已由另一个用户专门打开,或者您需要获得查看和写入其数据的权限。
我的代码:
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
}
}
答案 0 :(得分:1)
“C:\ Users \ Public”需要更改为您要访问的* .mdb文件的实际路径:
“C:\用户\ Public.mdb”
OR
“C:\用户\公共\ Something.mdb”
取决于您的数据库名称:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
或者它可能是* .accdb文件。如:
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等其他程序中打开文件,文件被标记为只读,或者安全权限不具有读取或写入权限,有时您会遇到此类问题。请注意,如果为像Users这样的组设置了“拒绝”权限(在文件系统/ NTFS中),那么它将覆盖所有其他权限,这样管理员就会受到拒绝权限的影响。
编辑:感谢您的评论,补充一点澄清。