Sunday, September 22, 2019

Can't open a password protected SQLite 3 database with C#

I'm having an issue using a password protected SQLite database, using System.Data.SQLite.




I'm using DB Browser for SQLite to create the database and set the password. With DB Browser I have no issues opening, entering the password viewing data, then closing the database.



So with .NET 4.6.2 and System.Data.SqLite 1.0.105.2 the following code snippet does not work I keep getting a "file is encrypted or is not a database" error.



namespace licensekeygeneration
{
using NLog;
using NLog.Extensions.AzureTableStorage;
using System;
using System.Data.SQLite;

using System.Linq;
using System.Windows;

/// Interaction logic for App.xaml
public partial class App : Application
{
/// Make sure that NLog is running
private static Logger logger = LogManager.GetCurrentClassLogger();

/// Run before the application starts up

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


If I remove the password from the database using DB Browser for SQLite and I change the following line:




string dataSource = @"Data Source=c:\users\fred\desktop\database.db;Version=3;Page Size=1024;";
SQLiteConnection conn = new SQLiteConnection(dataSource);


I get the information I expect and life is good, So am I missing something with System.Data.SQLite as I just can't get it to work as I expected.



If it matters I'm using Visual Studio 2017 on Windows 10 64Bit.



Thanks.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...