[英]Why can't I open a Sqlite database after setting a password using C# code?
[英]Can't open a password protected SQLite 3 database with C#
我使用System.Data.SQLite使用受密码保护的SQLite数据库时遇到问题。
我正在使用DB Browser for SQLite来创建数据库并设置密码。 使用数据库浏览器,我没有打开任何问题,输入密码查看数据,然后关闭数据库。
因此,对于.NET 4.6.2和System.Data.SqLite 1.0.105.2,以下代码片段不起作用我不断收到“文件已加密或不是数据库”错误。
namespace licensekeygeneration
{
using NLog;
using NLog.Extensions.AzureTableStorage;
using System;
using System.Data.SQLite;
using System.Linq;
using System.Windows;
/// <summary>Interaction logic for App.xaml</summary>
public partial class App : Application
{
/// <summary>Make sure that NLog is running</summary>
private static Logger logger = LogManager.GetCurrentClassLogger();
/// <summary>Run before the application starts up</summary>
void App_Startup(object sender, StartupEventArgs e)
{
try
{
// Set link to the SQLite database and grab the logging endpoint
string dataSource = @"Data Source=c:\users\fred\desktop\database.db;Version=3;Page Size=1024;Password=ABCD";
SQLiteConnection conn = new SQLiteConnection(dataSource);
DataContext LocalDB = new DataContext(conn);
// Sets the target for NLog in code
string strNlog = LocalDB.GetTable<TblS3Settings>().Where(item => item.StrSettingName.Equals("NlogEndPoint") && item.BoolIsValid.Equals(true)).ToList().FirstOrDefault().StrSettingValue;
var azureStorageTarget = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName("AzureTableStorage");
azureStorageTarget.ConnectionString = strNlog;
}
catch (Exception ex)
{
// Problem with the database or the connection so error out
MessageBox.Show("There is an issue with the internal database\n" + ex.Message, "Application", MessageBoxButton.OK, MessageBoxImage.Hand);
Current.Shutdown();
}
// Logging OK and we have an attached database so lets start
MainWindow.Show();
}
}
如果我使用DB Browser for SQLite从数据库中删除密码,我将更改以下行:
string dataSource = @"Data Source=c:\users\fred\desktop\database.db;Version=3;Page Size=1024;";
SQLiteConnection conn = new SQLiteConnection(dataSource);
我得到了我期望的信息,生活很美好,所以我错过了System.Data.SQLite的东西,因为我无法让它像我预期的那样工作。
如果重要的是我在Windows 10 64Bit上使用Visual Studio 2017。
谢谢。
设置密码接缝的可接受方式如下:
string dataSource = @"Data Source=c:\users\Fred\desktop\test.db;Version=3;Page Size=1024;";
SQLiteConnection conn = new SQLiteConnection(dataSource);
conn.Open();
conn.ChangePassword("ABCD");
conn.Close();
这个“应该”将数据库的密码设置为ABCD:
string dataSource = @"Data Source=c:\users\Fred\desktop\test.db;Version=3;Page Size=1024;Password=ABCD;";
SQLiteConnection conn = new SQLiteConnection(dataSource);
conn.Open();
这项工作似乎是正确的,但以下也有效:
string dataSource = @"Data Source=c:\users\Fred\desktop\test.db;Version=3;Page Size=1024;";
SQLiteConnection conn = new SQLiteConnection(dataSource);
conn.Open();
请注意,设置密码后,我可以在任何实用程序中打开受密码保护的数据库,而无需提供密码。
因此,除非我忽略了这一点,否则看起来System.Data.SQLite中的密码设置是可疑的。
尝试这个:
var command = connection.CreateCommand();
command.CommandText = "SELECT quote($password);";
command.Parameters.AddWithValue("$password", _password);
var quotedPassword = (string)command.ExecuteScalar();
command.CommandText = "PRAGMA key = " + quotedPassword;
command.Parameters.Clear();
command.ExecuteNonQuery();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.