You need to enable JavaScript to run this app. C#应用中SQLite数据库被判定为只读,修改文件夹属性仍报错如何解决
优惠活动
大模型
产品
解决方案
定价
更多

C#应用中SQLite数据库被判定为只读,修改文件夹属性仍报错如何解决

SQLite数据库写入操作触发只读报错排查记录

我尝试通过代码向SQLite数据库写入数据时,弹出报错提示:
报错弹窗截图
对应的写入逻辑代码如下:

private async Task InsertMapRecord(string mapName, string mapNotes)
{
    path = folder.Path;
    connStr = string.Format(connStrBase, path);
    try
    {
        SqliteConnection conn = new SqliteConnection(connStr);
        String query = "INSERT INTO CartographerMain " +
            "(MapName, MapNotes) " +
            "VALUES (@MapName, @MapNotes)";

        using (SqliteCommand cmd = new SqliteCommand(query, conn))
        {
            cmd.Parameters.AddWithValue("@MapName", mapName);
            cmd.Parameters.AddWithValue("@MapNotes", mapNotes);
            await conn.OpenAsync();
            int result = await cmd.ExecuteNonQueryAsync();

            if (result < 0)
            {
                MessageDialog dialog = new MessageDialog("Error inserting data into CartographerMain");
                await dialog.ShowAsync();
            }
        }
    }
    catch (SqliteException sqlex)
    {
        string sqlExcMsg = string.Format("{0} Inner Exception: {1} Stack Trace: {2}", sqlex.Message, sqlex.InnerException, sqlex.StackTrace);
        MessageDialog dialog = new MessageDialog(sqlExcMsg, "InsertMapRecord");
        await dialog.ShowAsync();
    }
}

我参考技术社区中关于调整SQLite数据库读写模式的相关解法,检查应用运行所在的文件夹属性,发现该文件夹被标记为只读:
文件夹只读属性截图
我取消了文件夹的只读标记、应用修改并保存配置:
取消只读属性后的设置截图
但重新运行插入逻辑后,仍然收到相同的报错,无法定位是哪项配置遗漏,导致数据库始终被判定为只读状态。

补充说明

针对评论中提出的数据库存储配置相关疑问,我的实现方式如下:

StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
string connStrBase = @"Data source={0}\Cartographer.db";
string connStr = string.Empty;
string path = string.Empty;

. . .

private async Task InsertMapRecord(string mapName, string mapNotes)
{
    path = folder.Path;
    connStr = string.Format(connStrBase, path);
    try
    {
        SqliteConnection conn = new SqliteConnection(connStr);
    . . .
}

我已将数据库文件添加到项目中,且将其“复制到输出目录”属性设置为“如果较新则复制”,本地项目路径下的数据库文件位置为:C:\Users\bclay\source\repos\CartographerYou\CartographerYou\Cartographer.db


内容的提问来源于stack exchange,提问作者B. Clay Shannon-B. Crow Raven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 22:45:03