C#给Sqlite数据库进行加密、修改密码_c#怎么给存入sqlite的数据加密
创始人
2025-01-15 08:34:25
0

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

    ///       /// 关闭数据库连接      ///       public void CloseConnection()      {          //销毁Command          if (dbCommand != null)          {              dbCommand.Cancel();          }          dbCommand = null;          //销毁Reader          if (dataReader != null)          {              dataReader.Close();          }          dataReader = null;          //销毁Connection          if (dbConnection != null)          {              dbConnection.Close();          }          dbConnection = null;        }        ///       /// 读取整张数据表      ///       /// The full table.      /// 数据表名称      public SQLiteDataReader ReadFullTable(string tableName)      {          string queryString = "SELECT * FROM " + tableName;  //获取所有可用的字段          return ExecuteQuery(queryString);      }        ///       /// 向指定数据表中插入数据      ///       /// The values.      /// 数据表名称      /// 插入的数值      public SQLiteDataReader InsertValues(string tableName, string[] values)      {          //获取数据表中字段数目          int fieldCount = ReadFullTable(tableName).FieldCount;          //当插入的数据长度不等于字段数目时引发异常          if (values.Length != fieldCount)          {              throw new SQLiteException("values.Length!=fieldCount");          }          string queryString = "INSERT INTO " + tableName + " VALUES (" + "'" + values[0] + "'";          for (int i = 1; i < values.Length; i++)          {              queryString += ", " + "'" + values[i] + "'";          }          queryString += " )";          return ExecuteQuery(queryString);      }        ///       /// 更新指定数据表内的数据      ///       /// The values.      /// 数据表名称      /// 字段名      /// 字段名对应的数据      /// 关键字      /// 关键字对应的值      /// 运算符:=,<,>,...,默认“=”      public SQLiteDataReader UpdateValues(string tableName, string[] colNames, string[] colValues, string key, string value, string operation)      {          // operation="=";  //默认          //当字段名称和字段数值不对应时引发异常          if (colNames.Length != colValues.Length)          {              throw new SQLiteException("colNames.Length!=colValues.Length");          }          string queryString = "UPDATE " + tableName + " SET " + colNames[0] + "=" + "'" + colValues[0] + "'";            for (int i = 1; i < colValues.Length; i++)          {              queryString += ", " + colNames[i] + "=" + "'" + colValues[i] + "'";          }          queryString += " WHERE " + key + operation + "'" + value + "'";            return ExecuteQuery(queryString);      }      ///       /// 更新指定数据表内的数据      ///       /// The values.      /// 数据表名称      /// 字段名      /// 字段名对应的数据      /// 关键字      /// 关键字对应的值      /// 运算符:=,<,>,...,默认“=”      public SQLiteDataReader UpdateValues(string tableName, string[] colNames, string[] colValues, string key1, string value1, string operation, string key2, string value2)      {          // operation="=";  //默认          //当字段名称和字段数值不对应时引发异常          if (colNames.Length != colValues.Length)          {              throw new SQLiteException("colNames.Length!=colValues.Length");          }          string queryString = "UPDATE " + tableName + " SET " + colNames[0] + "=" + "'" + colValues[0] + "'";            for (int i = 1; i < colValues.Length; i++)          {              queryString += ", " + colNames[i] + "=" + "'" + colValues[i] + "'";          }          //表中已经设置成int类型的不需要再次添加‘单引号’,而字符串类型的数据需要进行添加‘单引号’          queryString += " WHERE " + key1 + operation + "'" + value1 + "'" + "OR " + key2 + operation + "'" + value2 + "'";            return ExecuteQuery(queryString);      }          ///       /// 删除指定数据表内的数据      ///       /// The values.      /// 数据表名称      /// 字段名      /// 字段名对应的数据      public SQLiteDataReader DeleteValuesOR(string tableName, string[] colNames, string[] colValues, string[] operations)      {          //当字段名称和字段数值不对应时引发异常          if (colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length)          {              throw new SQLiteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length");          }            string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + "'" + colValues[0] + "'";          for (int i = 1; i < colValues.Length; i++)          {              queryString += "OR " + colNames[i] + operations[0] + "'" + colValues[i] + "'";          }          return ExecuteQuery(queryString);      }        ///       /// 删除指定数据表内的数据      ///       /// The values.      /// 数据表名称      /// 字段名      /// 字段名对应的数据      public SQLiteDataReader DeleteValuesAND(string tableName, string[] colNames, string[] colValues, string[] operations)      {          //当字段名称和字段数值不对应时引发异常          if (colNames.Length != colValues.Length || operations.Length != colNames.Length || operations.Length != colValues.Length)          {              throw new SQLiteException("colNames.Length!=colValues.Length || operations.Length!=colNames.Length || operations.Length!=colValues.Length");          }            string queryString = "DELETE FROM " + tableName + " WHERE " + colNames[0] + operations[0] + "'" + colValues[0] + "'";            for (int i = 1; i < colValues.Length; i++)          {              queryString += " AND " + colNames[i] + operations[i] + "'" + colValues[i] + "'";          }          return ExecuteQuery(queryString);      }          ///       /// 创建数据表      ///  +      /// The table.      /// 数据表名      /// 字段名      /// 字段名类型      public SQLiteDataReader CreateTable(string tableName, string[] colNames, string[] colTypes)      {          string queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "( " + colNames[0] + " " + colTypes[0];          for (int i = 1; i < colNames.Length; i++)          {              queryString += ", " + colNames[i] + " " + colTypes[i];          }          queryString += "  ) ";          return ExecuteQuery(queryString);      }        ///       /// Reads the table.      ///       /// The table.      /// Table name.      /// Items.      /// Col names.      /// Operations.      /// Col values.      public SQLiteDataReader ReadTable(string tableName, string[] items, string[] colNames, string[] operations, string[] colValues)      {          string queryString = "SELECT " + items[0];          for (int i = 1; i < items.Length; i++)          {              queryString += ", " + items[i];          }          queryString += " FROM " + tableName + " WHERE " + colNames[0] + " " + operations[0] + " " + colValues[0];          for (int i = 0; i < colNames.Length; i++)          {              queryString += " AND " + colNames[i] + " " + operations[i] + " " + colValues[0] + " ";          }          return ExecuteQuery(queryString);      }        ///       /// 本类log      ///       ///      public static void Log(string s)     {          Console.WriteLine("class SqLiteHelper:::" + s);      }  } 

}

 ②给窗体应用程序编写对应的控制功能:    

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace TestEncryptSqLite
{
public partial class Form1 : Form
{
//文件及其对应路径
private string _fileAndPath = null;

    private SQLiteConnection _con;     bool isCorrect = false;         //原密码是否正确       private SqliteHelper sql;      private string sqliteDatabasePWD = null;       ///      /// 文件路径     ///      public string DbFilePath { get =>@"E:\成品\EquipmentMonitoring1.db"; }      ///      /// 旧密码     ///      public string OriginalPassword { get; set; }      public string FileAndPath { get => _fileAndPath; set => _fileAndPath = value; }      ///      /// 修改密码     ///      /// 新密码     public void ChangePassword(string newPassword,string originalPassword)     {                 _con = new SQLiteConnection("Data Source=" + this.FileAndPath);         if (!string.IsNullOrEmpty(originalPassword))         {             try             {                 _con.SetPassword(originalPassword);                 _con.Open();                 isCorrect = true;                 if (isCorrect)                 {                     if (!string.IsNullOrEmpty(newPassword))                     {                         _con.ChangePassword(newPassword);                         _con.Close();                     }                     try                     {                         _con.Open();                     }                     catch (Exception ex)                     {                         throw new Exception("无法连接到数据库!" + ex.Message);                     }                 }             }             catch (Exception e)             {                 isCorrect = false;                 throw new Exception("无法连接到数据库!" + e.Message); ;             }          }         else         {             MessageBox.Show("请点击“选择文件”按钮,选择对应的数据库,选择对应的数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);         }                             }      public Form1()     {         InitializeComponent();     }      private void textBox2_TextChanged(object sender, EventArgs e)     {      }      private void button1_Click(object sender, EventArgs e)     {         string pwd = textBox1.Text.Trim();         string pwd2 = textBox2.Text.Trim();          if (!string.IsNullOrEmpty(pwd))         {             if (!string.IsNullOrEmpty(pwd2))             {                 ChangePassword(pwd2, pwd);                  MessageBox.Show("给 " + this.FileAndPath + " 修改密码成功!!!", "给Sqlite数据库修改密码成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                 textBox1.Text = "";                 textBox2.Text = "";             }             else             {                 MessageBox.Show("新密码不能为空,请检查后重新输入!!!", "Sqlite数据库修改提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);                 textBox2.Text = "";             }         }         else         {             MessageBox.Show("请输入正确的数据库原密码!", "Sqlite数据库原密码提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);             textBox1.Text = "";         }             }      private void button2_Click(object sender, EventArgs e)     {         _con = new SQLiteConnection("Data Source=" + this.FileAndPath);         if (!string.IsNullOrEmpty(textBox1.Text.Trim()))         {             if (textBox1.Text.Trim() == sqliteDatabasePWD)             {                 _con.SetPassword(textBox1.Text.Trim());             }             else             {                 MessageBox.Show("请在“打开数据库”按钮左侧输入正确的数据库密码!", "Sqlite数据库密码输入错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);             }                      }         else         {             MessageBox.Show("请点击“选择文件”按钮,选择对应的数据库,选择对应的数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);             MessageBox.Show("请在“打开数据库”按钮左侧输入正确的数据库密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);             return;                     }         try         {             _con.Open();         }         catch (Exception ex)         {             throw new Exception("无法连接到数据库!" + ex.Message);         }     }      private void button3_Click(object sender, EventArgs e)     {         if (!string.IsNullOrEmpty(textBox_filePath.Text))         {             if (!string.IsNullOrEmpty(textBox1.Text))             {                 sqliteDatabasePWD = textBox1.Text;                 Query();                 MessageBox.Show("查询数据成功,请看“查询成功”按钮左侧的标签内容", "查询成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);             }             else             {                 MessageBox.Show("请在“打开数据库”按钮左侧的输入框中输入正确的密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);             }                     }         else         {             MessageBox.Show("请点击“选择文件”按钮,选择对应的数据库", "提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);         }                     }       //插入数据     private void insertData()     {         try          {                // sql = new SqLiteHelper("data source=mydb.db");              sql = new SqliteHelper(this.FileAndPath,sqliteDatabasePWD);              //创建名为table1的数据表              sql.CreateTable("table1", new string[] { "ID", "Name", "Age", "Email" }, new string[] { "INTEGER", "TEXT", "INTEGER", "TEXT" });              //插入两条数据              sql.InsertValues("table1", new string[] { "1", "张三", "16", "Zhang@163.com" });              //sql.InsertValues("table1", new string[] { "2", "李四", "17", "Li4@163.com" });                //更新数据,将Name="张三"的记录中的Name改为"小三"              sql.UpdateValues("table1", new string[] { "Name" }, new string[] { "sunlei" }, "Name", "小三", "=");              //删除Name="小三"且Age=16的记录,DeleteValuesOR方法类似 

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

string[] { “1”, “张三”, “16”, “Zhang@163.com” });

            //sql.InsertValues("table1", new string[] { "2", "李四", "17", "Li4@163.com" });                //更新数据,将Name="张三"的记录中的Name改为"小三"              sql.UpdateValues("table1", new string[] { "Name" }, new string[] { "sunlei" }, "Name", "小三", "=");              //删除Name="小三"且Age=16的记录,DeleteValuesOR方法类似 

[外链图片转存中…(img-L8wtLW2Q-1715675622294)]
[外链图片转存中…(img-7WprbtTO-1715675622295)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

相关内容

热门资讯

专业讨论!德扑之星真破解套路(... 专业讨论!德扑之星真破解套路(辅助挂)软件透明挂(有挂了解)-哔哩哔哩;人气非常高,ai更新快且高清...
每日必看!智星德州菠萝外挂检测... 每日必看!智星德州菠萝外挂检测(辅助挂)软件透明挂(有挂教学)-哔哩哔哩1、玩家可以在智星德州菠萝外...
透视透明挂!轰趴十三水有后台(... 轰趴十三水有后台赢率提升策略‌;透视透明挂!轰趴十三水有后台(辅助挂)软件透明挂(有挂详情)-哔哩哔...
发现玩家!德扑ai助手软件(辅... 发现玩家!德扑ai助手软件(辅助挂)透视辅助(有挂教学)-哔哩哔哩;玩家在德扑ai助手软件中需先进行...
一分钟了解!x-poker辅助... 一分钟了解!x-poker辅助软件(辅助挂)辅助透视(有挂攻略)-哔哩哔哩1、每一步都需要思考,不同...
一分钟揭秘!德州最新辅助器(辅... 一分钟揭秘!德州最新辅助器(辅助挂)透视辅助(有挂攻略)-哔哩哔哩;德州最新辅助器最新版本免费下载安...
玩家攻略推荐!德州辅助(辅助挂... 玩家攻略推荐!德州辅助(辅助挂)辅助透视(有挂了解)-哔哩哔哩是由北京得德州辅助黑科技有限公司精心研...
揭秘真相!pokernow德州... 《揭秘真相!pokernow德州(辅助挂)辅助透视(有挂介绍)-哔哩哔哩》 pokernow德州软件...
五分钟了解!德州之星辅助器(辅... 五分钟了解!德州之星辅助器(辅助挂)辅助透视(有挂透明)-哔哩哔哩1、很好的工具软件,可以解锁游戏的...
推荐一款!pokermaste... 1、推荐一款!pokermaster有外挂(辅助挂)透视辅助(有挂教学)-哔哩哔哩;详细教程。2、p...