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程序下载