|
|
|
@ -0,0 +1,164 @@ |
|
|
|
|
using Microsoft.Data.Sqlite; |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Data; |
|
|
|
|
using System.IO; |
|
|
|
|
using System.Windows; |
|
|
|
|
using System.Windows.Markup; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DyuClient.Server.Helper |
|
|
|
|
{ |
|
|
|
|
internal class DbHelper |
|
|
|
|
{ |
|
|
|
|
static string password = "000000"; |
|
|
|
|
|
|
|
|
|
SqliteConnection connection; |
|
|
|
|
public DbHelper(string dbPath) |
|
|
|
|
{ |
|
|
|
|
this.connection = new SqliteConnection(new SqliteConnectionStringBuilder("data source=" + dbPath) |
|
|
|
|
{ |
|
|
|
|
Mode = SqliteOpenMode.ReadWriteCreate |
|
|
|
|
, |
|
|
|
|
Password = password |
|
|
|
|
}.ToString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public SqliteConnection CreateConnection() |
|
|
|
|
{ |
|
|
|
|
if(connection.State != System.Data.ConnectionState.Open) { |
|
|
|
|
connection.Open(); |
|
|
|
|
using (var command = connection.CreateCommand()) |
|
|
|
|
{ |
|
|
|
|
command.CommandText = "PRAGMA key = '" + password + "';"; |
|
|
|
|
command.CommandText += "PRAGMA case_sensitive_like = 1;"; |
|
|
|
|
command.ExecuteNonQuery(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return connection; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void CloseConnection() |
|
|
|
|
{ |
|
|
|
|
connection.Close(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int ExecuteNonQuery(string sql, SqliteParameter[] sqlParameters = null) |
|
|
|
|
{ |
|
|
|
|
int resultCount = -1; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
SqliteCommand cmd = new SqliteCommand(); |
|
|
|
|
cmd.Connection = connection; |
|
|
|
|
cmd.CommandText = sql; |
|
|
|
|
if (sqlParameters!=null) { |
|
|
|
|
cmd.Parameters.AddRange(sqlParameters); |
|
|
|
|
} |
|
|
|
|
resultCount = cmd.ExecuteNonQuery(); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
System.Windows.MessageBox.Show("数据插入失败"); |
|
|
|
|
} |
|
|
|
|
return resultCount; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//查询全部 |
|
|
|
|
public DataTable ExecuteQuery(String sql, SqliteParameter[] sqlParameters = null) |
|
|
|
|
{ |
|
|
|
|
DataTable table = new DataTable(); |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
SqliteCommand cmd = new SqliteCommand(); |
|
|
|
|
cmd.Connection = connection; |
|
|
|
|
cmd.CommandText = sql; |
|
|
|
|
if (sqlParameters != null) |
|
|
|
|
{ |
|
|
|
|
cmd.Parameters.AddRange(sqlParameters); |
|
|
|
|
} |
|
|
|
|
SqliteDataReader reader = cmd.ExecuteReader(); |
|
|
|
|
|
|
|
|
|
table.Load(reader); |
|
|
|
|
return table; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
System.Windows.MessageBox.Show("检索失败"); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
static string dbname = "test"; |
|
|
|
|
static string dbfile = dbname + ".db"; |
|
|
|
|
|
|
|
|
|
static string dbPath = "./" + dbfile; |
|
|
|
|
static string tmpFile = "tmp" + ".db"; |
|
|
|
|
static string tmpPath = "./" + tmpFile; |
|
|
|
|
public static void Export_Click(string dbname) |
|
|
|
|
{ |
|
|
|
|
var connection = new SqliteConnection(new SqliteConnectionStringBuilder("data source=" + dbPath) |
|
|
|
|
{ |
|
|
|
|
Mode = SqliteOpenMode.ReadWriteCreate |
|
|
|
|
, |
|
|
|
|
Password = password |
|
|
|
|
}.ToString()); |
|
|
|
|
connection.Open(); |
|
|
|
|
using (var command = new SqliteCommand($"ATTACH DATABASE '{dbname}.db' AS {dbname} KEY 'export';" + |
|
|
|
|
$"SELECT sqlcipher_export('{dbname}');" + |
|
|
|
|
$"DETACH DATABASE {dbname};", connection)) |
|
|
|
|
{ |
|
|
|
|
command.ExecuteNonQuery(); |
|
|
|
|
} |
|
|
|
|
connection.Close(); |
|
|
|
|
} |
|
|
|
|
public static void ChangePassword(string oldPassword, string newPassword) |
|
|
|
|
{ |
|
|
|
|
string baseConnectionString = $"Data Source={dbPath}"; |
|
|
|
|
var connectionString = new SqliteConnectionStringBuilder(baseConnectionString) |
|
|
|
|
{ |
|
|
|
|
Mode = SqliteOpenMode.ReadWriteCreate, |
|
|
|
|
Password = oldPassword |
|
|
|
|
}; |
|
|
|
|
using (var connection = new SqliteConnection(connectionString.ConnectionString)) |
|
|
|
|
{ |
|
|
|
|
connection.Open(); |
|
|
|
|
using (var command = connection.CreateCommand()) |
|
|
|
|
{ |
|
|
|
|
command.CommandText = "SELECT quote($newPassword);"; |
|
|
|
|
command.Parameters.AddWithValue("$newPassword", newPassword); |
|
|
|
|
var quotedNewPassword = (string)command.ExecuteScalar(); |
|
|
|
|
|
|
|
|
|
command.CommandText = "PRAGMA rekey = " + quotedNewPassword; |
|
|
|
|
command.Parameters.Clear(); |
|
|
|
|
command.ExecuteNonQuery(); |
|
|
|
|
|
|
|
|
|
connection.Close(); |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void CreateDb() |
|
|
|
|
{ |
|
|
|
|
if (!File.Exists(tmpPath) && !File.Exists(dbPath)) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
File.Create(tmpPath).Close(); |
|
|
|
|
|
|
|
|
|
var connection = new SqliteConnection(new SqliteConnectionStringBuilder("data source=" + tmpPath) |
|
|
|
|
{ |
|
|
|
|
Mode = SqliteOpenMode.ReadWriteCreate |
|
|
|
|
, |
|
|
|
|
Password = password |
|
|
|
|
}.ToString()); |
|
|
|
|
connection.Open(); |
|
|
|
|
using (var command = new SqliteCommand($"ATTACH DATABASE '{dbfile}' AS {dbname} KEY '{password}';" + |
|
|
|
|
$"SELECT sqlcipher_export('{dbname}');" + |
|
|
|
|
$"DETACH DATABASE {dbname};", connection)) |
|
|
|
|
{ |
|
|
|
|
command.ExecuteNonQuery(); |
|
|
|
|
} |
|
|
|
|
connection.Close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |