C#-创建/删除数据库文件时,始终抛出只读异常等

C#-创建/删除数据库文件时,始终抛出只读异常等,c#,xamarin.android,sqlite-net,C#,Xamarin.android,Sqlite Net,我正在尝试为sqlite数据库制作一个预加载C#程序。当我创建然后读取文件时,我会得到例外。当我删除并创建一个新的时,会出现异常。谁能告诉我我做错了什么 string dbPath = @"..\..\..\TidesDatabase\Assets\Database.3db"; bool exists = File.Exists (dbPath); if ( exists ) { File.SetAttributes( db

我正在尝试为sqlite数据库制作一个预加载C#程序。当我创建然后读取文件时,我会得到例外。当我删除并创建一个新的时,会出现异常。谁能告诉我我做错了什么

string dbPath = @"..\..\..\TidesDatabase\Assets\Database.3db";
        bool exists = File.Exists (dbPath);
        if ( exists )
        {
            File.SetAttributes( dbPath, FileAttributes.Normal );
            File.Delete( dbPath );
            File.Create( dbPath );
            File.SetAttributes( dbPath, FileAttributes.Normal );
        }
        else
        {
            File.Create( dbPath );
            File.SetAttributes( dbPath, FileAttributes.Normal );
        }
        var db = new SQLiteConnection( dbPath );
最后一行是引发异常的位置

堆栈跟踪:

在c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\preload\SqliteNet.cs中的SQLite.SQLiteConnection..ctor(字符串数据库路径、SQLiteOpenFlags openFlags、Boolean storeDateTimeAsTicks)处:第153行 在c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\preload\SqliteNet.cs中的SQLite.SQLiteConnection..ctor(字符串数据库路径,布尔存储日期时间标记)处:第114行 在c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\preload\Program.cs中的preload.Program.Main(字符串[]args)处:第40行 位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args) 位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()

中,我认为您没有从这样的路径获取文件的正确权限

要获取文件,应执行以下操作:

string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),
    "Database.3db");

如果您已经有一个数据库文件,您可以将其放在Assets文件夹中,然后可以使用
Assets.Open()

读取。注意:我假设所讨论的代码是1)在控制台应用程序中运行,而不是在移动操作系统上运行。2) 使用SQLiteNET(一种ORM)而不是直接使用SQLite

问题似乎是您正在创建一个空文件,然后希望sqlite将其作为数据库文件打开。如果数据库文件不存在,则需要让sqlite创建它。下面是一个例子:

string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Database.db3");

// If a db file doesn't exist, SQLite-Net will create it
var db = new SQLiteConnection (dbPath);

请发布您正在获取的异常的详细信息Narayana…屏幕截图的第一行包含更多文本,您可以将该文本复制并粘贴到您的问题中,这是您想要的额外文本吗?Narayana似乎“整个错误消息”只是“无法打开数据库文件”?这条路对吗?它有权限被你的程序访问吗?我正在笔记本电脑上使用预加载exe。接下来是android访问部分。对不起,我可能误解了你的问题。但是,你为什么用Android和MonoDroid来标记它呢?这个问题本身与Android无关。我使用的是sqliteNET,我对这些都不熟悉,所以我不确定有什么不同。第二件事是我没有看到SQLiteConnection.CreatFile方法。我从他们的网站上得到了我的SQLiteNET.cs。