官方文档
如果要更改已加密数据库的密钥,请发出 PRAGMA rekey 语句。遗憾的是,SQLite 在 PRAGMA 语句中不支持参数。 请改用 quote() 函数防止 SQL 注入。

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();

1.quote()函数结果返回到C#是带''一对单引号的,rekey 可以为字符。
2.在创建数据库连接的时候不带密码或者密码错误也能正常使用函数,但是无法增删改查和修改数据库参数。
3.上面代码最后ExecuteNonQuery()修改密码成功返回的是-1。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐