【问题标题】:Can't open a password protected SQLite 3 database with C#无法使用 C# 打开受密码保护的 SQLite 3 数据库
【发布时间】:2017-06-27 21:00:59
【问题描述】:

我在使用受密码保护的 SQLite 数据库时遇到问题,使用 System.Data.SQLite。

我正在使用 DB Browser for SQLite 创建数据库并设置密码。使用 DB 浏览器打开,输入密码查看数据,然后关闭数据库没有问题。

因此,对于 .NET 4.6.2 和 System.Data.SqLite 1.0.105.2,以下代码 sn-p 不起作用我不断收到“文件已加密或不是数据库”错误。

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 64 位上使用 Visual Studio 2017。

谢谢。

【问题讨论】:

  • Rene,这也许是正确的。但是...为什么总是有一个但是?

标签: c# sqlite passwords


【解决方案1】:

SQLiteConnection 和 DB Browser for SQLite 使用不同类型的加密。您必须使用 SQLiteConnection 本身加密数据库。不幸的是,您之后无法使用 DB Browser

encrypt with c#

我相信除了自己在 DB Browser 中实施加密之外,没有其他解决方案。已经有关于这个话题的讨论,开发者似乎没有计划实现:discussion here

【讨论】:

  • "SQLiteConnection 和 DB Browser for SQLite 使用不同类型的加密。"你救了我。自从过去 2 小时以来,我一直在摸不着头脑
【解决方案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 中的密码设置似乎是可疑的。

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多