首页 文章

无法使用C#打开受密码保护的SQLite 3数据库

提问于
浏览
1

我使用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 .

谢谢 .

3 回答

  • 0

    SQLite的SQLiteConnection和DB Browser使用不同种类的加密 . 您必须使用SQLiteConnection本身加密数据库 . 不幸的是,之后您无法使用数据库浏览器

    encrypt with c#

    我相信没有解决方案,而不是自己在DB Browser中实现加密 . 已经有关于这个主题的讨论,开发人员似乎没有计划实现:discussion here

  • 2

    设置密码接缝的可接受方式如下:

    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中的密码设置是可疑的 .

  • 0

    试试这个:

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

相关问题