当前位置: 首页 > news >正文

C# + SQLiteExpert 进行(cipher)加密数据库开发+Costura.Fody 清爽发布

      一:让 SQLiteExpert  支持(cipher)加密数据库

         SQLiteExpert  作为SQlite 的管理工具,默认不支持加密数据库的,使其成为支持(cipher)加密数据库的管理工具,需要添加e_sqlcipher.dll (复制到SQLiteExpertPro32/64.exe 同级目录即可,从哪里来的这个问题:你可以直接往后面下载链接下,也可以跟随文章知道从哪里来的。最好是知道从哪里来的,也许我放链接的版本在未来有新版本更替,我也就不更新下载链接了。),并在其设置里面进行勾选就能激活加密数据库支持。如下图所示(几个关键点已经标记),需要注意的是SQLiteExpert  32位/64位需要与e_sqlcipher.dll 32位/64位匹配,否则在32位版的SQLiteExpert  是看不到64位的e_sqlcipher.dll。

加入成功后,打开或者新建数据库都有加密选项

(新建数据库)

(打开加密数据库)

这样到此可以完整的处理了SQLiteExpert 支持加密

        由于SQLiteExpert 只有windows 版本,linux 平台的cipher加密支持库也就不打包了。对于跨平台的SQLite gui 也许类似,上传的压缩包就只有windows平台dll。

下载链接在顶部,压缩包结构如下:

二. .Net C# 连接(cipher)加密的SQlite数据库

nuget

1、SQLitePCLRaw.bundle_e_sqlcipher

2、Microsoft.Data.Sqlite

3、Costura.Fody (如果不需要考虑发布洁癖(单文件,全部外围DLL聚合),就没必要。)

直接debug,生成一堆内容,我随便建立了个VS 工程(sqlitedemo) debug目录大致如下:

runtime 内容如下:(也就是各种平台的(cipher)sqlite lib),前面的e_sqlcipher.dll取材就是从这里取得。

debug 生成的很多,也就是刚才说的发布生成洁癖的问题,如果你在意,发布时候选择输出好平台,runtime可以自动减少,但是依旧会生成其他dll。

那就采用Costura.Fody

编辑项目内 FodyWeavers.xml 文件(nuget过了会自动生成该文件),文件内容修改如下:

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"><Costura ><Unmanaged64Assemblies>e_sqlciphere_sqlite3</Unmanaged64Assemblies><Unmanaged32Assemblies>e_sqlciphere_sqlite3</Unmanaged32Assemblies></Costura>
</Weavers>

再编辑 解决方案文件(右边第一箭头那里双击)

主要语句加入 (我选择的x64版本的Exe)

 <RuntimeIdentifier>win-x64</RuntimeIdentifier>

        这样,右键点击 解决方案->重新生成解决方案 (这里要注意,要重新整个解决方案才行。更改解决方案文件,直接debug会有问题。)

        最终生成输出内容如下:

其他乱七八糟的dll没有了,清爽吧?! 

再来看相关C# 连接Sqlite 操作的代码:先上SqliteHelper,顺便也推荐一下 Cody AI生成的,我用了很长时间,免费且好用(面向编程)。

    public class SQLiteHelper{private readonly string _connectionString;public SQLiteHelper(string dbPath, string password = null){var builder = new SqliteConnectionStringBuilder{DataSource = dbPath,Mode = SqliteOpenMode.ReadWriteCreate};if (!string.IsNullOrEmpty(password)){builder.Password = password;}_connectionString = builder.ConnectionString;}public int ExecuteNonQuery(string sql, List<SqliteParameter> parameters = null){using (var connection = new SqliteConnection(_connectionString)){connection.Open();using (var command = connection.CreateCommand()){command.CommandText = sql;if (parameters != null){command.Parameters.AddRange(parameters);}return command.ExecuteNonQuery();}}}public T ExecuteScalar<T>(string sql, List<SqliteParameter> parameters = null){using (var connection = new SqliteConnection(_connectionString)){connection.Open();using (var command = connection.CreateCommand()){command.CommandText = sql;if (parameters != null){command.Parameters.AddRange(parameters);}var result = command.ExecuteScalar();return (T)Convert.ChangeType(result, typeof(T));}}}public List<T> ExecuteQuery<T>(string sql, List<SqliteParameter> parameters = null) where T : new(){var result = new List<T>();using (var connection = new SqliteConnection(_connectionString)){connection.Open();using (var command = connection.CreateCommand()){command.CommandText = sql;if (parameters != null){command.Parameters.AddRange(parameters);}using (var reader = command.ExecuteReader()){while (reader.Read()){var item = new T();for (int i = 0; i < reader.FieldCount; i++){var property = typeof(T).GetProperty(reader.GetName(i), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);if (property != null && !reader.IsDBNull(i)){property.SetValue(item, Convert.ChangeType(reader.GetValue(i), property.PropertyType));}}result.Add(item);}}}}return result;}public void ExecuteTransaction(Action<SqliteConnection> action){using (var connection = new SqliteConnection(_connectionString)){connection.Open();using (var transaction = connection.BeginTransaction()){try{action(connection);transaction.Commit();}catch{transaction.Rollback();throw;}}}}}
}

调用如下:

SQLiteHelper 构造函数,第一个参数数据库路径文件全名,第二个参数数据库密码

   SQLiteHelper SLH = new SQLiteHelper("D:\\Manage\\Documents\\db", "TEST");const int batchSize = 100000;const string insertSql = "INSERT INTO Student (F_ID, F_Name) VALUES (@F_ID, @F_Name)";SLH.ExecuteTransaction(connection =>{using var command = connection.CreateCommand();command.CommandText = insertSql;command.Parameters.Add("@F_ID", SqliteType.Text);command.Parameters.Add("@F_Name", SqliteType.Text);for (int i = 0; i < batchSize; i++){command.Parameters["@F_ID"].Value = Guid.NewGuid().ToString();command.Parameters["@F_Name"].Value = $"John{i}";command.ExecuteNonQuery();}});

密码错误则在这里报错:

这样既可以C# 开发加密数据库又可以在外部用外部工具直接编辑加密数据库的环境就达成了。

后话:

        选择SQLiteExpert 是界面要黑一点,可能不是最好用的,但黑色看起来眼睛舒服一点,欢迎推荐一些。

http://www.lryc.cn/news/456324.html

相关文章:

  • MySQL 8.0 新特性之自增变量持久化
  • 网站建设公司哪家好?好的网站建设公司应该有哪些特别之处?
  • 香山南湖架构分析--FE
  • 【Linux的那些事】shell命名及Linux权限的理解
  • Visual Studio 2022 配置 Boost 库
  • 逻辑回归(下): Sigmoid 函数的发展历史
  • 快速理解mQ(三)——RabbitMQ 各种交换机的区别与应用
  • 【五分钟学会】YOLO11 自定义数据集从训练到部署
  • Redis学习(十二)连接数不足报错及分析修复:ERR max number of clients reached.
  • 八股文面试题总结(包含主流的面试经典题)
  • 一分钟掌握 Java22 新特性
  • 西安凭借入驻企业展示科技“硬”实力的数字媒体产业园
  • 【网络安全】利用XSS、OAuth配置错误实现token窃取及账户接管 (ATO)
  • 浙江所有省级医院体检报告查询上线浙里办!
  • 支付宝支付Java+uniapp支付宝小程序
  • Linux-磁盘优化的几个思路
  • 【第三版 系统集成项目管理工程师】第15章 组织保障
  • 从编程视角看生命、爱、自由、生活的排列顺序
  • Lumerical——属性编辑窗口的详解
  • 08实战篇:972应用题(2024)思路解析
  • 解决应用程序启动失败问题:由于找不到d3dx9_43.dll文件,如何快速有效地恢复和修复缺失的DLL组件
  • Ubuntu——双系统Ubuntu22.04系统安装和基础配置
  • stm32定时器中断和外部中断
  • LeetCode 每日一题 2024/9/30-2024/10/6
  • Redis篇(最佳实践)(持续更新迭代)
  • 详细介绍pandas 在python中的用法
  • 八字命理测算系统开发-源码搭建
  • Python批量读取mat文件
  • UE4 材质学习笔记05(凹凸偏移和视差映射/纹理压缩设置)
  • 基于贝叶斯决策的 CAD 程序设计方案