Search code examples
vb.netsqlitesystem.data.sqlite

Set password for SQLite v3 database


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!


Solution

  • 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).