System.Data.SQLite中的SQLiteConnection类提供了一个ChangePassword的方法,这个方法并非来自其父类DbConnection,而是在SQLiteConnection类中定义的。不禁让人感叹,这个开源驱动做的真不错。

使用System.Data.SQLite 下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

得到System.Data.SQLite.dll添加到工程引用;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
 
namespace SetSqlitePassword
{
    class DbOperator
    {
        private SQLiteConnection _con;
 
        /// <summary>
        /// 文件路径
        /// </summary>
        public string DbFilePath { get; set; }
 
        /// <summary>
        /// 旧密码
        /// </summary>
        public string OriginalPassword { get; set; }
 
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="newPassword">新密码</param>
        public void ChangePassword(string newPassword)
        {
            _con = new SQLiteConnection();
            _con.ConnectionString = "Data Source=" + this.DbFilePath;
            if (this.OriginalPassword.Length > 0)
            {
                _con.ConnectionString += ";Password=" + this.OriginalPassword;
            }
            try
            {
                _con.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("无法连接到数据库!"+ ex.Message);
            }
            _con.ChangePassword(newPassword);
            _con.Close();
        }
    }
}

Demo程序下载

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐