簡體   English   中英

SQLite 錯誤 14:如果在根文件夾中,則“無法打開數據庫文件”

[英]SQLite Error 14: 'unable to open database file' if in root folder

我的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"完美配合,但這不是很便攜,所以我需要使用它自己的目錄。

有人知道嗎?

更新:我在這里再次嘗試使用教程,但在異常頁面中遇到了相同的錯誤。

“處理請求時發生未處理的異常。SqliteException:SQLite 錯誤 14:'無法打開數據庫文件'。Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(int rc, sqlite3 db)”

更新:當我嘗試時,我收到“System.UnauthorizedAccessException: 'Access to the path 'C:\\Program Files\\IIS Express\\test.txt' is denied.'”

字符串路徑 = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");

using (FileStream fs = File.Create(path)) { Byte[] info = new UTF8Encoding(true).GetBytes("這是文件中的一些文本。"); // 向文件中添加一些信息。 fs.Write(info, 0, info.Length); }

如何使用以下內容來構建指向您的數據庫文件的路徑?:

// 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);

請注意,您需要在DatabaseContext包含以下using語句:

using System.IO;
using System.Reflection;

您需要將數據庫放在App_Data文件夾下。 所有 SQLite 所需的dll 都需要在bin文件夾中。 此外, SQLite.Interop.dll需要位於 bin 下的x64 和 x86文件夾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM