gpt4 book ai didi

c# - ASP.NET CORE Web 应用程序抛出 SQLiteException : unable to open database file

转载 作者:行者123 更新时间:2023-12-02 08:31:00 25 4
gpt4 key购买 nike

我有一个 SQLite 数据库,并将其上传到我的服务器 [托管在 azure 中]。但是当我尝试访问它时,它抛出SQLiteException:无法打开数据库文件

我的连接字符串是这样的 -

string connectionString = "数据源=https:\\mywebsite.azurewebsites.net\\DB\\persondb.db; 版本=3"

我也尝试过这个 -

string connectionString = "数据源=~/DB/persondb.db; Version=3";

这是我的完整方法的代码 -

        public static List<PersonDetails> GetPersons()
{
// string connectionString = "Data Source=https:\\mywebsite.com\\DB\\persondb.db; Version=3";
string connectionString = "Data Source=~/DB/persondb.db; Version=3";

using (IDbConnection cnn = new SQLiteConnection(connectionString))
{
var output = cnn.Query<PersonDetails>
("SELECT * FROM persons",
new DynamicParameters());
return output.ToList();
}
}

如何解决这个问题?

最佳答案

引用About SQLite

SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process. SQLite reads and writes directly to ordinary disk files.

您可以在 ASP.NET Web 项目中使用以下代码:

    string path = Server.MapPath("~/DB");
string connectionString = $"Data Source={path}/demo.db; Version=3";

using (var con = new SQLiteConnection(connectionString))
{
con.Open();
var cmd = new SQLiteCommand("SELECT SQLITE_VERSION()", con);
return cmd.ExecuteScalar().ToString();
}

代码Server.MapPath("~/DB")将找到项目中DB文件夹的物理路径,然后连接字符串应该可以工作。

关于c# - ASP.NET CORE Web 应用程序抛出 SQLiteException : unable to open database file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61049882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com