免费版Sqlie是不提供设置密码功能的,经过查阅资料最终找到了解决方案
方案一,从sqlite源码入手,据说sqlite源码已经提供了加密的接口,只是免费版没有实现,可以参考这位仁兄的博客以了解详情:http://www.cnblogs.com/hiloves/archive/2010/04/25/1719749.html
方案二,使用System.Data.SQLite这个ADO.Net驱动写好的方法来修改密码,详情请参见这位仁兄的博客:http://www.watch-life.net/net-tip/sqlite-encrypted.html。
System.Data.SQLite中的SQLiteConnection类提供了一个ChangePassword的方法,这个方法并非来自其父类DbConnection,而是在SQLiteConnection类中定义的。不禁让人感叹,这个开源驱动做的真不错。
我写了个Winform程序以方便修改密码:
主要功能类代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/*
* author:Joey Zhao
* date:2010-12-28 22:04:46
*/
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();
}
}
}
|