sqlcipher 3.x
可使用 SQLiteStudio 打开
密码需要在每次执行前使用??
command.CommandText = "PRAGMA key=X9bEekoXXP"; command.ExecuteNonQuery();
//类库需要 一下两个, 版本号要一致
Microsoft.Data.Sqlite? ?2.2.4
SQLitePCLRaw.bundle_sqlcipher? 1.1.14
public List<Dictionary<string, object>> GetList(string sql, Dictionary<string, object> parameters)
{
using var connection = new SqliteConnection(ConnectionString);
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "PRAGMA key=X9bEekoXXP";
command.ExecuteNonQuery();
command.CommandText = sql;
if (parameters != null)
foreach (var p in parameters)
{
command.Parameters.AddWithValue("$" + p.Key, p.Value);
}
using var reader = command.ExecuteReader();
var list = new List<Dictionary<string, object>>();
while (reader.Read())
{
var item = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
item.Add(reader.GetName(i), reader.GetValue(i));
}
list.Add(item);
}
return list;
}
sqlcipher 4.x
?使用? DB Browser for SQLite 工具打开 ,SQLiteStudio 无法打开,
密码可以通过 连接字符串传递
首次连接带密码创建数据库,需同时建表,密码才能生效
string dbName = Path.Combine(Environment.CurrentDirectory, "SampleDB.db");
string connStr = new SqliteConnectionStringBuilder()
{
DataSource = dbName,
Mode = SqliteOpenMode.ReadWriteCreate,
Password = "admin"
}.ToString();
var db = new SqlSugarClient(new ConnectionConfig()
{
ConfigId = 1,
DbType = DbType.Sqlite,
ConnectionString = connStr,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
using (var connection = new SqliteConnection(connStr))
{
connection.Open();
var com = connection.CreateCommand();
com.CommandText = @"CREATE TABLE Users (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
UserName TEXT(16),
CreateTime TEXT NOT NULL
);";
com.ExecuteNonQuery();
}
类库???
?Microsoft.Data.Sqlite.Core" Version="5.0.9"? ? SQLitePCLRaw.bundle_e_sqlcipher" Version="2.0.4"? ? ?SqlSugarCore" Version="5.0.4"?
|