首页 > 解决方案 > 如果没有这样的数据库,如何检查 Xamarin 中是否存在 SQLite 数据库文件并创建一个新的数据库文件?

问题描述

我正在尝试编写一种检查 SQLite 文件存在的方法。我下载了一个名为 System.Data.SQLite.Core 的用于 SQLite 功能的 Nuget 包。我为该方法编写了以下代码:

 string path = "ReportData.db";

        private void Create_db()
        {
            if (!System.IO.File.Exists(path))
            {
                SQLiteConnection.CreateFile(path);
                using (var sqlite = new SQLiteConnection(@"Data Source=" + path))
                {
                    sqlite.Open();
                    string sql = "create table temp(date varchar(20),activity varchar(1000))";
                    SQLiteCommand command = new SQLiteCommand(sql, sqlite);
                    command.ExecuteNonQuery();
                    sqlite.Close();
                }
            }
        }

我不知道在哪里创建我的 SQLite 数据库文件。当我在按钮中使用此方法时,应用程序进入中断模式并说:

System.UnauthorizedAccessException:'访问路径“/ReportData.db”被拒绝。'

请帮我。

标签: xamarin

解决方案


推荐阅读