gpt4 book ai didi

c# - SQLite3 - "Unable to open database"

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

我创建了一个 SqlLite 3 db 文件并将其放置在我的项目的 App_Data 文件夹(ASP.Net Web API)中。我似乎无法连接到它。异常消息很模糊,我无法确定出了什么问题。权限?文件未找到?不知道。

我在 web.config 中创建了以下 connectionString 条目:

 <connectionStrings>
<add name="LiteConnection" connectionString="Data Source=~\App_Data\repository.s3db;Version=3;" />

当我尝试 Open() 连接时抛出异常。我开始怀疑也许 IISExpress (VS2012) 无法写入这个目录或其他东西?

  private string _dbLitePath = ConfigurationManager.ConnectionStrings["LiteConnection"].ConnectionString;

...
public IDbConnection GetConnection()
{
try
{
if (_connection == null)
_connection = new SQLiteConnection(_dbLitePath);

if (_connection.State != ConnectionState.Open)
{
_connection.ConnectionString = _dbLitePath;
_connection.Open(); // kaaaaasplat!
}
}
catch (SQLiteException e)
{

}
return _connection;
}

最佳答案

尝试将连接字符串更改为:

 connectionString="Data Source=|DataDirectory|\repository.s3db

DataDirectory is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.

引用Where is DataDirectory

关于c# - SQLite3 - "Unable to open database",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16698877/

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