'How to connect to sqlite database with password
I have a sqlite database and I want to connect from my C# program using a password for the database. I am using Navicat and I set encrypt database file with password "test" and then by code my connection string is:
_connection = new SQLiteConnection("Data Source=MedExpress.db;Version=3;Password=\"test\";");
or
_connection = new SQLiteConnection("Data Source=MedExpress.db;Version=3;Password=test;");
But this does not work.
The error is: File opened that is not a database file
file is encrypted or is not a database
I can connect to the database without a password like this:
_connection = new SQLiteConnection("Data Source=MedExpress.db;Version=3;");
My question is how can I set a password to a sqlite database and connect from C# program using System.Data.SQLite
Solution 1:[1]
you can provide password via connection string;
Data Source=filename;Version=3;Password=myPassword;
Also, have a look at his link
hope it helps
Solution 2:[2]
Its very long time ago, but here is my solution that work's with LITEDB using connectionstring and password. But remember you need to set password in your db first. You can use this tool (LITE DB EXPLORER ) to create and manage your databases. https://github.com/JosefNemec/LiteDbExplorer
In App Config Add your connection string like this example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="LiteDB" connectionString="Filename=.\Databases\Data.db"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
And in C# code behind:
private static string LoadConnectionString(string id = "LiteDB")
{
try {
return ConfigurationManager.ConnectionStrings[id].ConnectionString + ";password=your_pass";
}
catch (Exception loadConnectionStringError)
{
Console.WriteLine("loadConnectionStringError: " + loadConnectionStringError.ToString());
return null;
}
}
Solution 3:[3]
It's not a problem of a connection string. If you have not add any table to database since the encrypted database has been created for the first time. It doesn't permit to connect the database with password although we don't know it's bug or not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AnarchistGeek |
| Solution 2 | Douglas Flores |
| Solution 3 | JJ Lee |
