更新
This commit is contained in:
63
CreateEncrptyDB.md
Normal file
63
CreateEncrptyDB.md
Normal file
@@ -0,0 +1,63 @@
|
||||
```csharp
|
||||
|
||||
//internal class UserInfo
|
||||
//{
|
||||
// [PrimaryKey] [AutoIncrement] public int Id { get; set; }
|
||||
|
||||
// public string password { get; set; }
|
||||
//}
|
||||
private static readonly string path = "ArchiWBSCode.db";
|
||||
private static readonly string path1 = "ts.db";
|
||||
|
||||
private static readonly string pathS = "ArchiWBSCode.sqlite";
|
||||
private static void CreateEncryptedDb(string newfilename, string password)
|
||||
{
|
||||
var conn = new SqliteConnectionStringBuilder
|
||||
{
|
||||
DataSource = newfilename,
|
||||
Mode = SqliteOpenMode.ReadWriteCreate,
|
||||
Password = password
|
||||
}.ToString(); //使用这个方式设置密码,避免sql注入
|
||||
//创建SQLite连接
|
||||
using (SqliteConnection connection = new SqliteConnection(conn))
|
||||
{
|
||||
if (connection.State == ConnectionState.Closed)
|
||||
{
|
||||
connection.Open();
|
||||
var createTableSqlStr =
|
||||
@"CREATE TABLE if not exists ""Alarm"" ( ""Id"" INTEGER NOT NULL, ""AlarmName"" TEXT, ""AlarmTypeId"" INTEGER, PRIMARY KEY ( ""Id"" ) );";
|
||||
var result = connection.Execute(createTableSqlStr); //使用Dapper执行sql语句创建表
|
||||
//Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int UpdatePassword(string fileName, string oldPassword, string newPassword)
|
||||
{
|
||||
var connectionString = new SqliteConnectionStringBuilder
|
||||
{
|
||||
DataSource = fileName,
|
||||
Mode = SqliteOpenMode.ReadWriteCreate,
|
||||
Password = oldPassword
|
||||
}.ToString();
|
||||
|
||||
using (var connection = new SqliteConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "SELECT quote($newPassword);";
|
||||
command.Parameters.AddWithValue("$newPassword", newPassword);
|
||||
var quotedNewPassword = command.ExecuteScalar() as string;
|
||||
|
||||
command.CommandText = "PRAGMA rekey = " + quotedNewPassword;
|
||||
command.Parameters.Clear();
|
||||
int x = command.ExecuteNonQuery();
|
||||
return x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string oldPassword = "123qwe";
|
||||
private string newPassword = "abcd";
|
||||
```
|
||||
Reference in New Issue
Block a user