C#给Sqlite数据库进行加密、修改密码

2024-07-14 1124阅读

C#给Sqlite数据库进行加密、修改密码

C#给Sqlite数据库进行加密、修改密码

既有适合小白学习的零基础资料,也有适合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  

}

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

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方法类似

C#给Sqlite数据库进行加密、修改密码

C#给Sqlite数据库进行加密、修改密码

既有适合小白学习的零基础资料,也有适合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%以上物联网嵌入式知识点,真正体系化!

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

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

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]