NetSQLite 1.3.4
dotnet add package NetSQLite --version 1.3.4
NuGet\Install-Package NetSQLite -Version 1.3.4
<PackageReference Include="NetSQLite" Version="1.3.4" />
<PackageVersion Include="NetSQLite" Version="1.3.4" />
<PackageReference Include="NetSQLite" />
paket add NetSQLite --version 1.3.4
#r "nuget: NetSQLite, 1.3.4"
#:package NetSQLite@1.3.4
#addin nuget:?package=NetSQLite&version=1.3.4
#tool nuget:?package=NetSQLite&version=1.3.4
NetSQLite — 纯 C# 实现的 SQLite 引擎
基于 sqlite-src-3530400(SQLite 3.53.4)源码,使用 .NET 10 / C# 从零实现的
嵌入式 SQL 数据库引擎。数据库文件格式与 SQLite 3 完全兼容。
特性
- 文件格式兼容:100 字节文件头、b-tree 页布局、记录序列类型、溢出页、 空闲页链表、回滚日志均按官方文件格式规范实现
- 内置 FreeSql ORM 支持:通过 ADO.NET 兼容层直接接入,无需任何 SQLite 提供程序包
- 存储引擎
Pager:页缓存、原子提交、崩溃恢复(热日志自动回放)、跨连接变更检测BTree:表 b-tree(rowid 键)与索引 b-tree、页分裂、溢出链、懒式删除收缩RecordCodec:序列类型编解码 + 无分配的编码记录比较(unsafe 指针遍历)
- SQL 引擎:词法分析器、递归下降解析器(SELECT/JOIN/子查询/GROUP BY/ ORDER BY/LIMIT、INSERT…SELECT、UPDATE、DELETE、DDL、PRAGMA)、 INDEX/ROWID 查找计划、嵌套循环 JOIN、分组聚合、参数绑定
安装
dotnet add package NetSQLite
快速上手
原生 API
using NetSQLite;
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);
FreeSql ORM
using FreeSql;
using NetSQLite.Adonet;
var fsql = new FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite,
() => new NetSqliteConnection("Data Source=mydb.db"))
.UseAutoSyncStructure(true) // 自动建表
.Build();
// 插入
fsql.Insert(new User { Name = "张三", Age = 28 }).ExecuteAffrows();
// 批量插入(事务内)
fsql.Transaction(() =>
{
fsql.Insert(users).ExecuteAffrows();
});
// 查询
var list = fsql.Select<User>().Where(u => u.Age > 18).OrderBy(u => u.Name).ToList();
// 更新
fsql.Update<User>(new { Age = 30 }).Where(u => u.Name == "张三").ExecuteAffrows();
// 删除
fsql.Delete<User>().Where(u => u.Id == 2).ExecuteAffrows();
性能(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 |
关键优化:
- 语义快路径:COUNT(*) 走 O(页数) 内部页遍历计数; MAX/MIN(rowid 别名) 走 O(log n) b-tree 端点定位——二者均反超原生
- 投影下推:只解码查询引用的列;rowid-only 查询零 payload IO
- 原地 UPDATE:同长记录字节覆写 + 仅变更列维护索引
测试
dotnet test tests/NetSQLite.Tests # 44 个测试全通过
dotnet run --project bench/NetSQLite.Bench -c Release # 性能对比
覆盖范围:varint 全边界往返、记录编解码与序型选择、B-tree 万次随机插入/删除 与 SortedDictionary 模型等价性(跨 4 轮提交)、50KB 溢出载荷、索引前缀查找、 崩溃恢复(手工构造热日志)、DDL 回滚、参数命名绑定、多语句脚本、 ADO.NET 兼容层 CRUD/事务/约束。
已知限制
- WAL 模式未实现(仅回滚日志);WITHOUT ROWID 表暂不支持
- CHECK / 外键约束语法可解析但不强制执行
- 触发器、视图、窗口函数、CTE 未实现
- 删除采用懒式再平衡(页空即回收),不主动合并半满页
| Product | Versions 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. |
-
net10.0
- FreeSql (>= 3.5.311)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.3.2: Fix FreeSql UseAutoSyncStructure auto-create table.
- ExecuteScalar returns null on empty result (was DBNull, caused false "table exists" detection)
- CreateDbParameter no longer double-adds to parameter collection (fixed named-param binding shift)
1.3.1: Prepare multi-statement compatibility fix (FreeSql CodeFirst).
1.3.0: Full FreeSql ORM integration.