Files
06-Note/CreateEncrptyDB.md
sherlockforrest bf2ed2e31f 更新
2023-06-20 09:22:53 +08:00

64 lines
2.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
```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";
```