简体   繁体   English

我收到“SQLite 错误 14:'无法打开数据库文件'”。

[英]I'm receiving “SQLite Error 14: 'unable to open database file'.”

I am writing an ASP.NET web API on .NET Framework 4.7.2.我正在 Z303CB0EF9EDB9082D61BBBE5825D972.AZ 框架上编写 ASP.NET web API。 I would like to have a SQLite database in a file on my harddrive.我想在我的硬盘驱动器上的文件中有一个 SQLite 数据库。 Then I could access the database using a SqliteConnection.然后我可以使用 SqliteConnection 访问数据库。

In my code, I call var db = new SqliteConnection("Data Source=testDB.sqlite");在我的代码中,我调用var db = new SqliteConnection("Data Source=testDB.sqlite"); to create the SqliteConnection.创建 SqliteConnection。 Then I call db.Open();然后我调用db.Open(); . . This function throws a Microsoft.Data.Sqlite.SqliteException containing the error "SQLite Error 14: 'unable to open database file'."此 function 引发 Microsoft.Data.Sqlite.SqliteException,其中包含错误“SQLite 错误 14:'无法打开数据库文件'”。

I tried to create the testDB.sqlite file manually, but I wasn't sure where to put it.我尝试手动创建 testDB.sqlite 文件,但不知道该放在哪里。 I am not asking the SqliteConnection to create any directories.我没有要求 SqliteConnection 创建任何目录。 I am not writing a UWP app (because ASP.NET is different from UWP, correct?).我不是在编写 UWP 应用程序(因为 ASP.NET 与 UWP 不同,对吗?)。 I've tried both a relative path and an absolute path.我已经尝试过相对路径和绝对路径。 I even deleted the entire project and used the Visual Studio project creation wizard to regenerate it.我什至删除了整个项目并使用 Visual Studio 项目创建向导重新生成它。 I do not know what else to try.我不知道还能尝试什么。

I've created a minimal reproducible example here .我在这里创建了一个最小的可重现示例。 You should be able to just pop this right into Visual Studio.您应该可以直接将其弹出到 Visual Studio 中。 Besides the plumbing, the example is literally only 2 lines long.除了管道,这个例子实际上只有 2 行长。 Does anyone have any ideas what I'm doing wrong?有谁知道我做错了什么?

For your project try to install dependece: Microsoft.EntityFrameworkCore.Sqlite and correct connection string in your code like this:对于您的项目,请尝试安装依赖:Microsoft.EntityFrameworkCore.Sqlite 并在您的代码中正确连接字符串,如下所示:

var db = new SqliteConnection("Data Source=file:testDB.sqllite;Mode=ReadWrite;");
db.Open();

Microsoft.Data.Sqlite is using filename in Data Sourse as URI: https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open Microsoft.Data.Sqlite 使用数据源中的文件名作为 URI: https://www.sqlite.org/c3ref/open.html#urifile3

I created sqllite database with SQLliteStudio: https://github.com/pawelsalawa/sqlitestudio/releases我用 SQLliteStudio 创建了 sqllite 数据库: https://github.com/pawelsalawa/sqlitestudio/releases

Added testDB.sqllite in project and set property "Copy to Output directory":"Copy if newer".在项目中添加 testDB.sqllite 并设置属性“复制到 Output 目录”:“如果较新则复制”。

After those steps "Open" worked success在这些步骤之后“打开”工作成功

In my case, I'd accidentally mixed up the versions of the Nuget packages added to projects within my solution.就我而言,我不小心混淆了在我的解决方案中添加到项目中的 Nuget 包的版本。

I had a reference to the following inside the .csproj :我在.csproj中引用了以下内容:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1"
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.1" />

Updateding packages to:将软件包更新为:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.10"
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.10" />

Resolved this error for us inside a UWP project.在 UWP 项目中为我们解决了这个错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM