Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法打开数据库文件(误用)SQLLiteSyncConnection_C#_Sqlite_Xamarin.forms_Uwp - Fatal编程技术网

C# 无法打开数据库文件(误用)SQLLiteSyncConnection

C# 无法打开数据库文件(误用)SQLLiteSyncConnection,c#,sqlite,xamarin.forms,uwp,C#,Sqlite,Xamarin.forms,Uwp,我遇到异常,无法打开数据库文件:[path]尝试打开我的SQLite连接时误用 我正在创建一个Xamarin.Forms应用程序,并在UWP中进行调试,这就是我得到异常的地方 my data store类的构造函数创建连接和表: internal static string DBPath { get { const string FILE_NAME = "TheRandomizer.db3"; if (Device.RuntimePlatform

我遇到异常,无法打开数据库文件:[path]尝试打开我的SQLite连接时误用

我正在创建一个Xamarin.Forms应用程序,并在UWP中进行调试,这就是我得到异常的地方

my data store类的构造函数创建连接和表:

internal static string DBPath
{
    get
    {
        const string FILE_NAME = "TheRandomizer.db3";
        if (Device.RuntimePlatform == Device.Android)
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), FILE_NAME);
        }
        else
        {
             return Path.Combine(ApplicationData.Current.LocalFolder.Path, FILE_NAME);
        }
    }
}

public SqliteDataStore()
{
    _database = new SQLiteAsyncConnection(DBPath, SQLiteOpenFlags.Create);
    // Fails trying to perform this action:
    _database.CreateTableAsync<GeneratorTable>().Wait();
    _database.CreateTableAsync<TagTable>().Wait();
}
可以查看完整的源代码

堆栈跟踪:

在System.Threading.Tasks.Task.WaitInt32毫秒时, CancellationToken CancellationToken位于 System.Threading.Tasks.Task.Wait at TheRandomizer.Services.SqliteDataStore..ctor位于 TheRandomizer.ViewModels.BaseViewModel.get_数据存储位于 TheRandomizer.ViewModels.GeneratorListViewModel.ExecuteLoadItems命令

使现代化 我已经在本地测试了您的代码,并看到了实际发生的行为,经过一些调试后,我认为我可能有两个原因:

首先,构造函数只有SQLiteOpenFlags.Create标志。显然,这不会给您任何其他权限,包括读/写权限。相反,您可以完全忽略第二个参数:

_database = new SQLiteAsyncConnection(DBPath);
或包括显式读写标志我还包括FullMutex标志,因为它建议用于异步连接:

_database = new SQLiteAsyncConnection(
     DBPath, 
     SQLiteOpenFlags.Create | 
     SQLiteOpenFlags.FullMutex | 
     SQLiteOpenFlags.ReadWrite );
在DB中创建GeneratorTable表时出现第二个问题。SQLite不知道如何存储Version属性,因为它是自定义GeneratorVersion类型。因此,您可能必须将其分解为简单属性或添加[Ignore]属性

原始答案 我已经检查了您的源代码,发现您正在尝试将数据库存储在Environment.SpecialFolder.Personal文件夹中。对于UWP,这实际上解析为C:\Users\$Username$\Documents,UWP应用程序无权访问它,因为它在沙箱中运行,并且无权访问它

相反,您必须使用应用程序的数据文件夹,您可能实际上打算:

Path.Combine(ApplicationData.Current.LocalFolder.Path, FILE_NAME);

DBPath的值是多少?@Jason我根据Martin的想法更改了DBPath后添加了代码。尝试了此操作,但不幸得到了相同的异常。@Lance我已用正确的解决方案更新了我的答案:-