Set password for SQLite v3 database

折月煮酒 提交于 2020-07-05 05:13:33

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/41425759/set-password-for-sqlite-v3-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!