NetSQLite 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NetSQLite --version 1.0.0
                    
NuGet\Install-Package NetSQLite -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetSQLite" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetSQLite" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="NetSQLite" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetSQLite --version 1.0.0
                    
#r "nuget: NetSQLite, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NetSQLite@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetSQLite&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=NetSQLite&version=1.0.0
                    
Install as a Cake Tool

NetSQLite — 纯 C# 实现的 SQLite 引擎

基于 sqlite-src-3530400(SQLite 3.53.4)源码,使用 .NET 10 / C# 从零实现的 嵌入式 SQL 数据库引擎。数据库文件格式与 SQLite 3 完全兼容

特性

  • 文件格式兼容:100 字节文件头、b-tree 页布局、记录序列类型、溢出页、 空闲页链表、回滚日志均按官方文件格式规范实现
  • 存储层Storage/
    • Pager:页缓存、原子提交、崩溃恢复(热日志自动回放)、跨连接变更检测
    • BTree:表 b-tree(rowid 键)与索引 b-tree、页分裂、溢出链、懒式删除收缩
    • RecordCodec:序列类型编解码 + 无分配的编码记录比较(unsafe 指针遍历)
    • BtPageOps:页内空闲块分配/合并/碎片整理(Buffer.MemoryCopy 快照搬运)
  • SQL 层Sql/):词法分析器(关键字完美分派)、递归下降解析器 (SELECT/JOIN/子查询/GROUP BY/ORDER BY/LIMIT、INSERT…SELECT、UPDATE、DELETE、 CREATE/DROP TABLE·INDEX、BEGIN/COMMIT/ROLLBACK、PRAGMA)
  • 执行器Engine/):行上下文绑定、比较亲和性规则、INDEX/ROWID 等值查找计划、 嵌套循环 JOIN(INNER/LEFT/CROSS)、分组聚合、标量/聚合内建函数、参数绑定 (?/?N/:name

使用

using var db = SqliteConnection.Open("mydb.sqlite");
db.Execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
using var ins = db.Prepare("INSERT INTO users (name) VALUES (?)");
ins.Bind(1, "alice").Execute();
foreach (var row in db.Execute("SELECT * FROM users").Rows)
    Console.WriteLine(row[0].Int);

性能(Release,Windows,.NET 10,20K 行数据集)

工作负载 NetSQLite sqlite3 原生 比值
INSERT ×20,000(单事务) 105,694 r/s 185,327 r/s 1.75x
POINT SELECT by rowid ×20,000 58,425 r/s 13,842 r/s 0.24x(快 4 倍)
COUNT(*) ×50 418,760,469 r/s 258,391,256 r/s 0.62x(快 1.6 倍)
MAX(id) ×50 1,730,702,665 r/s 304,720,114 r/s 0.18x(快 5.7 倍)
UPDATE ×5,000 305 r/s 770 r/s 2.52x

关键优化:

  • 语义快路径SELECT COUNT(*) 走 O(页数) 内部页遍历计数; MAX/MIN(rowid 别名) 走 O(log n) b-tree 端点定位——二者均反超原生
  • 投影下推:只解码查询引用的列;rowid-only 查询零 payload IO
  • 原地 UPDATE:同长记录字节覆写 + 仅变更列维护索引

点查得益于 .NET JIT 与无锁读缓存反超原生;全表扫描差距来自逐行解码与 表达式树遍历(后续可通过向量化与列裁剪优化)。

ORM 集成

通过内置 ADO.NET 兼容层可直接接入 FreeSql / Dapper 等 ORM:

csharp var fsql = new FreeSqlBuilder() .UseConnectionFactory(FreeSql.DataType.Sqlite, () => new NetSqliteConnection("Data Source=app.db")) .Build();

详见 docs/使用文档.md 第 18 章。

测试

dotnet test tests/NetSQLite.Tests          # 29 个测试:格式兼容/B-tree 模糊/
dotnet run --project bench/NetSQLite.Bench -c Release   # 性能对比

覆盖范围:varint 全边界往返、记录编解码与序型选择、B-tree 万次随机插入/删除 与 SortedDictionary 模型等价性(跨 4 轮提交)、50KB 溢出载荷、索引前缀查找、 崩溃恢复(手工构造热日志)、DDL 回滚、参数命名绑定、多语句脚本。

已知限制

  • WAL 模式未实现(仅回滚日志);WITHOUT ROWID 表暂不支持
  • CHECK / 外键约束语法可解析但不强制执行
  • 触发器、视图、窗口函数、CTE 未实现
  • 删除采用懒式再平衡(页空即回收),不主动合并半满页
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.4 48 8/24/2026
1.3.1 54 8/23/2026
1.3.0 51 8/23/2026
1.2.0 54 8/23/2026
1.1.0 51 8/23/2026
1.0.0 62 8/23/2026
0.9.0 51 8/23/2026

1.0.0: 正式版。
     - ADO.NET 兼容层(DbConnection/Command/Reader/Transaction/Factory)
     - FreeSql 通过 UseConnectionFactory 直接接入
     - COUNT(*)/MAX/MIN(rowid 别名) 快路径
     - 投影下推、原地 UPDATE、多语句批次执行
     - PRAGMA table_info / user_version / synchronous