You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用System.Data.SQLite执行加密SQLite数据库查询时报错求助

解决System.Data.SQLite查询加密数据库报错“file is not a database”的问题

从你的描述来看,连接成功但查询时抛出SQLite error (26): file is not a database异常,核心原因大概率是加密格式/配置不兼容——虽然密码正确能建立连接,但System.Data.SQLite对数据库的加密解析方式和DB Browser for SQLite等工具不一致,导致无法正确识别数据库结构。

先梳理你的现有代码问题点

你的连接字符串构建代码:

string conn = "Data Source=" + filename + ";Version=3;";
if (passphrase != null) {
    conn += "Page Size=1024;Password=" + passphrase + "";
}
_mDbConnection = new SQLiteConnection(conn);
_mDbConnection.Open();

这里有几个可能的配置冲突点,下面给出针对性的解决方案:

解决方案1:统一加密格式配置

System.Data.SQLite默认使用SQLCipher加密,而部分工具可能使用了旧版SQLite加密格式或者不同的加密参数。你可以尝试在连接字符串中添加加密格式相关参数,强制兼容:

string conn = "Data Source=" + filename + ";Version=3;";
if (passphrase != null) {
    conn += "Page Size=1024;Password=" + passphrase + ";Legacy Format=True;Cipher=SQLCipher;";
}
  • Legacy Format=True:兼容旧版SQLite加密格式
  • Cipher=SQLCipher:明确指定使用SQLCipher加密标准,和主流工具对齐

解决方案2:移除手动指定的Page Size,让驱动自动匹配

你手动设置了Page Size=1024,但如果数据库实际的页面大小和这个值不匹配,会导致驱动无法正确解析数据库结构。建议先去掉这个参数,让System.Data.SQLite自动检测页面大小:

string conn = "Data Source=" + filename + ";Version=3;";
if (passphrase != null) {
    conn += "Password=" + passphrase + ";Legacy Format=True;";
}

如果业务必须指定页面大小,先在DB Browser for SQLite中查看数据库的实际页面大小(打开数据库后,点击「数据库结构」→「数据库属性」查看),再对应设置连接字符串中的参数。

解决方案3:使用SetPassword方法代替连接字符串传参

有些情况下,直接通过连接字符串传递密码可能存在编码问题,改用SQLiteConnection.SetPassword方法传递字节数组形式的密码会更可靠:

string conn = "Data Source=" + filename + ";Version=3;";
_mDbConnection = new SQLiteConnection(conn);
if (passphrase != null) {
    // 将密码转为UTF8字节数组,避免编码不一致问题
    byte[] passwordBytes = Encoding.UTF8.GetBytes(passphrase);
    _mDbConnection.SetPassword(passwordBytes);
}
_mDbConnection.Open();

额外验证步骤

  1. 确认DB Browser for SQLite中使用的加密方式:打开数据库时,查看是否选择了「SQLCipher」加密选项,确保和System.Data.SQLite的加密标准一致;
  2. 尝试用System.Data.SQLite重新加密一个空数据库,再用DB Browser打开验证,以此确认加密配置的一致性,排除数据库本身的异常。

你的错误日志参考:

SQLite error (26): file is not a database
抛出异常:'System.Data.SQLite.SQLiteException' in System.Data.SQLite.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical">
<TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier>
<Description>Unhandled exception</Description>
<AppDomain>dbtool.exe</AppDomain>
<Exception>
<ExceptionType>System.Data.SQLite.SQLiteException, System.Data.SQLite, Version=1.0.106.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139</ExceptionType>
<Message>file is not a database file is not a database</Message>
<StackTrace> at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String&amp;amp;amp; strRemain) at System.Data.SQLite.SQLiteCommand.BuildNextCommand() at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteReader()

内容的提问来源于stack exchange,提问作者oleole

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:06:57