ASP.Net Core:SQLite错误14:如果在根文件夹中,则“无法打开数据库文件”

时间:2019-01-28 12:58:32

标签: c# asp.net sqlite

我的DatabaseContext有:

string physicalPath = "Filename=" + Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Data"), "Database.db");
optionsBuilder.UseSqlite(physicalPath);

我的Startup有:

using (var client = new DatabaseContext())
{
   client.Database.EnsureCreated();
}

它与"D:\\Database.db"完美配合,但这不是很容易移植,因此我需要使用其自己的目录。

有人有什么主意吗?

更新: 我再次尝试使用教程here,并在异常页面中遇到了相同的错误。

  

”处理请求时发生未处理的异常。   SqliteException:SQLite错误14:“无法打开数据库文件”。   Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(int rc,   sqlite3 db)“

更新:尝试时出现“ System.UnauthorizedAccessException:'对路径'C:\ Program Files \ IIS Express \ test.txt'的访问被拒绝。”

  

字符串路径= Path.Combine(Directory.GetCurrentDirectory(),   “ test.txt”);

     

使用(FileStream fs = File.Create(path)){Byte [] info = new   UTF8Encoding(true).GetBytes(“这是文件中的一些文本。”); //添加   一些信息到文件。 fs.Write(info,0,info.Length); }

2 个答案:

答案 0 :(得分:0)

如何使用以下内容构造指向您的DB文件的路径?

// Gets the current path (executing assembly)
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// Your DB filename    
string dbFileName = "stuff.db"; 
// Creates a full path that contains your DB file
string absolutePath = Path.Combine(currentPath, dbFileName);

请注意,您需要在using中包含以下DatabaseContext语句:

using System.IO;
using System.Reflection;

答案 1 :(得分:0)

您需要将数据库放置在 App_Data 文件夹下。所有所需的SQLite dll 必须位于 bin 文件夹中。此外,SQLite.Interop.dll还必须位于bin的 x64和x86 文件夹中。

相关问题