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

是否可在Node.js中运行.NET类库调用System.Data.SQLite?

可行性结论

这个需求完全可以实现,不存在技术层面的阻碍。你可以根据自己的场景选择原生驱动适配或者.NET互操作两种方案,后者对System.Data.SQLite的兼容性是100%,不会出现加密算法不匹配的问题。

实现方案

方案1:Node.js原生驱动直连(性能优先)

如果你使用的System.Data.SQLite是基于SQLCipher编译的版本,完全可以直接用支持多加密套件的Node.js SQLite驱动直接连接,不需要额外依赖.NET环境。
操作步骤:

  • 安装支持多加密算法的SQLite驱动包,优先选better-sqlite3-multiple-ciphers,同步API性能比异步的sqlite3包好很多:
    npm install better-sqlite3-multiple-ciphers
  • 编写连接代码,注意要匹配System.Data.SQLite默认的加密配置,旧版System.Data.SQLite默认用RC4加密,需要手动指定兼容模式:
const Database = require('better-sqlite3-multiple-ciphers');
// 打开数据库文件
const db = new Database('./你的数据库文件路径', { readonly: true });
// 配置加密兼容参数
db.pragma("cipher = 'rc4'");
db.pragma("legacy = 4");
// 传入数据库密码
db.pragma(`key = '你设置的数据库密码'`);
// 执行测试查询,能正常返回结果就代表连接成功
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all();
console.log('数据库内的表:', tables);
db.close();

如果运行时提示“文件不是合法的数据库”,说明你用的System.Data.SQLite是集成官方闭源SEE加密模块的版本,没有公开的算法兼容参数,直接换下面的方案2即可。

方案2:.NET互操作调用System.Data.SQLite(兼容性优先)

这个方案直接复用你现有的System.Data.SQLite类库,完全不需要适配加密规则,不会出现算法不匹配的问题,适合生产环境使用。
操作步骤:

  • 确认本机安装了和你所用System.Data.SQLite版本匹配的.NET运行时(.NET Framework 4.x、.NET Core 3.1、.NET 6+都支持)
  • 安装Node.js的.NET互操作依赖edge-js,这个包可以让Node.js直接在进程内加载.NET程序集并调用方法:
    npm install edge-js
  • 编写调用代码,注意要把你本地的System.Data.SQLite.dll路径填到引用配置里,dll的架构(32位/64位)必须和你当前运行的Node.js进程架构一致,不然会加载失败:
const edge = require('edge-js');

// 封装读取加密数据库的方法
const queryEncryptedDb = edge.func({
  source: function () {/*
    using System.Data.SQLite;
    using System.Collections.Generic;

    async (input) => {
      string dbPath = (string)input.dbPath;
      string password = (string)input.password;
      string sql = (string)input.sql;
      List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
      // 直接用System.Data.SQLite的连接串,和你.NET项目里的写法完全一致
      using (SQLiteConnection conn = new SQLiteConnection($"Data Source={dbPath};Password={password};"))
      {
        await conn.OpenAsync();
        using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
        using (SQLiteDataReader reader = await cmd.ExecuteReaderAsync())
        {
          while (await reader.ReadAsync())
          {
            Dictionary<string, object> row = new Dictionary<string, object>();
            for (int i = 0; i < reader.FieldCount; i++)
            {
              row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i);
            }
            result.Add(row);
          }
        }
      }
      return result;
    }
  */},
  references: [
    // 替换成你本地System.Data.SQLite.dll的实际路径
    "./deps/System.Data.SQLite.dll"
  ]
});

// 调用示例
queryEncryptedDb({
  dbPath: "./你的加密数据库文件路径",
  password: "你的数据库密码",
  sql: "SELECT * FROM 你要查询的表名 LIMIT 10"
}, (err, data) => {
  if (err) throw err;
  console.log('查询结果:', data);
});
常见问题说明
  • 用方案1时如果连接失败,不要反复试密码,先回到.NET环境用同一个System.Data.SQLite库验证密码正确性、文件是否损坏,再调整加密相关的pragma参数,比如部分新版System.Data.SQLite默认用AES加密,把cipher参数改成aes-256-cbc再试即可。
  • 用方案2时如果提示dll加载失败,先检查Node.js是32位还是64位,对应替换同架构的System.Data.SQLite.dll即可,也可以把System.Data.SQLite的所有依赖dll(比如SQLite.Interop.dll)都放到和引用dll同目录下。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 13:33:11