1

My application uses a database stored in a file available via network. So far, I've been using a MS-Access file (.accdb), but I'm trying to migrate to SQLite Version 3 (.db3).

I added SQLite NuGet package to my project and created a SQLite database using SQLiteStudio. I refactored my database objects to work with System.Data.SQLite.SQLiteConnection instead of System.Data.OleDb.OleDbConnection and it worked well.

However, my previous accdb database was password protected, and I don't know how to apply a password over my current SQLite database.

Can anyone teach me ho to do it? Thanks in advance!

VBobCat
  • 2,527
  • 4
  • 29
  • 56
  • 1
    Perhaps [this](http://stackoverflow.com/questions/1381264/password-protect-a-sqlite-db-is-it-possible) will help you – Wudge Jan 02 '17 at 11:39

1 Answers1

6

I followed the link which Wudge kindly appointed in comment above, and it works, but I'd rather clarify what need to be done:

  1. To set a password to an unprotected database:

    Dim conn = New SQLite.SQLiteConnection(
        "Data Source=C:\yourFolder\yourDB.db3;Version=3;")
    conn.Open()
    conn.ChangePassword("password")
    conn.Close()
    
  2. To open a password-protected database:

    Dim conn = New SQLite.SQLiteConnection(
        "Data Source=C:\yourFolder\yourDB.db3;Version=3;")
    conn.SetPassword("password")
    conn.Open()
    conn.Close()
    

    or

    Dim conn = New SQLite.SQLiteConnection(
        "Data Source=C:\yourFolder\yourDB.db3;Version=3;Password=password;")
    conn.Open()
    conn.Close()
    
  3. To remove password from a password-protected database:

    Dim conn = New SQLite.SQLiteConnection(
        "Data Source=C:\yourFolder\yourDB.db3;Version=3;Password=password;")
    conn.Open()
    conn.ChangePassword(String.Empty)
    conn.Close()
    

PS. The open source database manager SQLiteStudio is able to open files which were password-protected that way, as long as you choose System.Data.SQLite instead of Sqlite 3 as your database type. (Requires v 3.1.1, the current version).

Community
  • 1
  • 1
VBobCat
  • 2,527
  • 4
  • 29
  • 56
  • Hi VBobCat, I'm doing the same in my vb.net project, but It does't work and I don't understand what I'm getting wrong. I'm using System.Data.SQLite (downloaded offccial sqlite-netFx46-setup-bundle-x86-2015-1.0.101.0.exe). Managers used DB.Browser.for.SQLite-3.10.1 and SQliteStudio too. I've tryed setting password from code and from DB Browser This is my code: Dim mDbConnection as String = "Data Source= C:\TestDB.s3db;Version=3; Dim cnn As New SQLiteConnection(mDbConnection) cnn.SetPassword("test") cnn.Open() cnn.Close() – Anchor Jan 17 '18 at 10:04
  • This exception: File opened that is not a database file file is encrypted or is not a database – Anchor Jan 17 '18 at 10:55
  • No idea. But I had some trouble in the past, thinking I'd set up a password and in fact it was not set. In SQLiteStudio, right-click your database root icon and choose "Edit the Database" so you can see if a password was actually set. Beyond that, I'm clueless. – VBobCat Jan 17 '18 at 11:50
  • Even if I've set the password in DB Broswer, SQLiteStudio doesn't like it and If I actually would to set a password in SQLiteStudio I don't find how can I do it. So: I 've opend db without password setted by code (similar above code), I immediately changed Password to set it (cnn.ChangePassword("test")), and now everytime I want to open db the string "Data Source= C:\TestDB.s3db;Version=3;Password=test" works. And after this I can manage the db with SQLiteStudio. Thank you! – Anchor Jan 17 '18 at 13:44