无法打开数据库文件Windows Store App 8.1。

5
我正在尝试在我的Windows 8.1 Modern UI应用程序中实现数据库。我已经成功地为Windows Phone 8.1应用程序创建了这个,但是在新的Windows 8.1应用程序中不起作用。
当我实例化SQLiteConnection时,我遇到了一个SQLiteException,消息是“无法打开数据库文件:MyAppBase.db(CannotOpen)”。
public static DatabaseHelper Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new DatabaseHelper();
            }

            return _instance;
        }
    }

    public DatabaseHelper()
    {
        _connection = new SQLiteConnection(DATABASE_NAME);

        _connection.CreateTable<UserEntity>();
    }

我遵循以下步骤: 1. 将“sqlite-net”添加到Nuget参考中; 2. 在项目引用中选中“SQLite for Windows Runtime(Windows 8.1)”和“Microsoft Visual C++ 2013 Runtime Package for Windows”; 3. 将生成目标设置为x86。
如何使它起作用?
2个回答

9

SQLite需要一个路径来创建数据库,而不仅仅是数据库名称。

可以尝试像这样:

string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, DATABASE_NAME);

_connection = new SQLite.SQLiteConnection(path)

希望这可以帮到您。

啊,当然,它必须与应用程序一起使用。谢谢!出于某些愚蠢的原因,我以为我可以使用“C:\temp\”或另一个文件夹。 - Ryan Russon

0

使用

string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, databasePath);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接