简体   繁体   English

Microsoft Access错误找不到.mdb,但数据库为.accdb

[英]Microsoft Access error cannot find .mdb but database is .accdb

I'm doing a sample program accessing a Microsoft Access database. 我正在做一个示例程序,用于访问Microsoft Access数据库。 It is a .accdb file. 这是一个.accdb文件。 The name of the Database is ACRONYM_DB.accdb and has one data table called ACRONYM. 数据库的名称为ACRONYM_DB.accdb,并且具有一个称为ACRONYM的数据表。 My code is below : 我的代码如下:

string currentLoc = System.AppDomain.CurrentDomain.BaseDirectory.ToString();

ObservableCollection<Acronym.Acronym> acrOC = new ObservableCollection<Acronym.Acronym>();
string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + currentLoc + "\\Assets\\ACRONYM_DB.accdb;Jet OLEDB:Database Password=password";

OleDbConnection MyConn = new OleDbConnection(ConnStr);
MyConn.Open();
OleDbCommand myCommand = MyConn.CreateCommand();
myCommand.CommandText = "SELECT * FROM ACRONYM_DB.ACRONYM WHERE ACRONYM_NAME=" + acrName;
OleDbDataReader myReader = myCommand.ExecuteReader();

On the Execute reader line I'm gettting the error : 在执行阅读器行中,我得到了错误:

System.Data.OleDb.OleDbException was unhandled by user code
  HResult=-2147467259
  Message=Could not find file 'C:\Users\Mark\Desktop\release\ACRONYM_DB.mdb'.
  Source=Microsoft Office Access Database Engine
  ErrorCode=-2147467259
  StackTrace:
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.OleDb.OleDbCommand.ExecuteReader()
       at AcronymFinder.Model.Database.AcronymDatabase.HistoricalDef(String acrName) in c:\Users\Mark\Documents\Visual Studio 2013\Projects\AcronymFinder\AcronymFinder\Model\Database\AcronymDatabase.cs:line 28
       at AcronymFinder.ViewModel.MainViewModel.set_SelectedAcronym(Acronym value) in c:\Users\Mark\Documents\Visual Studio 2013\Projects\AcronymFinder\AcronymFinder\ViewModel\MainViewModel.cs:line 315
  InnerException: 

I know the error has got to be with the query I'm using but what is it I need to do differently? 我知道错误与我正在使用的查询有关,但是我需要做些什么呢? Also I am using a 64 bit version of Access 2013. 另外,我正在使用Access 2013的64位版本。

Do not include the database name in the SELECT statement: 不要在SELECT语句中包括数据库名称:

myCommand.CommandText = "SELECT * FROM ACRONYM WHERE ACRONYM_NAME=" + acrName;

That change should stop the db engine from complaining that it can't find ACRONYM_DB.mdb . 所做的更改应阻止db引擎抱怨它找不到ACRONYM_DB.mdb

However the revised statement could still fail, with a different error, if your ACRONYM_NAME field is text datatype. 但是,如果您的ACRONYM_NAME字段为text数据类型,则修改后的语句仍可能失败,并出现其他错误。 If it is text, you could avoid a missing parameter value complaint by including quotes before and after the value of acrName : 如果是文本,则可以通过在acrName值的前后加上引号来避免遗漏参数值投诉:

myCommand.CommandText = "SELECT * FROM ACRONYM WHERE ACRONYM_NAME='" + acrName + "'";

But, really, a parameter query would be a better approach because you would be protected against SQL injection and you wouldn't need to bother about quotes if acrName is a text value. 但是,实际上,参数查询将是一种更好的方法,因为可以防止SQL注入,并且如果acrName是文本值,则不必担心引号。

You forgot to quote your value: 您忘记了报价:

    myCommand.CommandText = "[..snip..]ACRONYM_NAME='" + acrName + "'";
                                                    ^-----------^^^^^^^

without the quotes, you're doing 没有引号,您正在做

ACRONYM_NAME=foo

instead of 代替

ACORNYM_NAME='foo'

which means the DB is looking for some unknown/non-existent FIELD named foo , instead of string 'foo' . 这意味着数据库正在寻找一些名为foo未知/不存在的FIELD ,而不是字符串'foo'

将Microsoft Office 16.0对象库添加到您的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MS Access / accdb“无法打开数据库”错误(C#) - MS Access/accdb “Cannot open database” error (C#) 无法访问受密码保护的Microsoft accdb数据库Microsoft.ACE.OLEDB.12.0 - Cannot access password protected Microsoft accdb database Microsoft.ACE.OLEDB.12.0 无法使用 ACE OLEDB 访问 Microsoft Access (.accdb) 文件,但可以使用 JET OLEDB 访问 (.mdb) 文件 - Can't access Microsoft Access (.accdb) files with ACE OLEDB, but can access (.mdb) files with JET OLEDB c敏锐的Web服务访问Microsoft数据库(mdb)? - c sharp webservice access microsoft database (mdb)? 使用行选择将数据库 (.mdb / .accdb) 导出到 .csv - Export DataBase (.mdb / .accdb) to .csv with row selection 新建 C# 项目无法打开 ACCDB microsoft 数据库文件 - New C# project cannot open ACCDB microsoft database file 正在加密“ Microsoft Access数据库” .accdb文件可能使其被黑客入侵 - Is encrypting “Microsoft Access database ” .accdb file could get it hacked mdb文件oledbconnection引发“ Microsoft Access数据库引擎无法打开或写入文件…” - mdb file oledbconnection throws “The Microsoft Access database engine cannot open or write to the file …” 使用LINQ和C#查询Microsoft Access MDB数据库 - Query Microsoft Access MDB Database using LINQ and C# OLEDB连接到Access数据库(accdb) - OLEDB connection to Access Database (accdb)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM