C# 如何使用密码连接到sqlite数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9586502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:00:43  来源:igfitidea点击:

How to connect to sqlite database with password

c#sqlitenavicat

提问by ddarellis

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:

我有一个 sqlite 数据库,我想使用数据库的密码从我的 C# 程序连接。我正在使用 Navicat 并使用密码“test”设置加密数据库文件,然后通过代码我的连接字符串是:

_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

错误是: 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

我的问题是如何为 sqlite 数据库设置密码并使用 C# 程序连接 System.Data.SQLite

采纳答案by Prince Jea

This is the connection string with password

这是带密码的连接字符串

Data Source=filename;Version=3;Password=myPassword;

As you stated that you use navicat to set the sqlite encryption. Encryption means that you've encrypted the database it's different from setting a password to a database..

正如您所说,您使用 navicat 来设置 sqlite 加密。加密意味着您已经加密了数据库,这与为数据库设置密码不同。

in setting a password to a database try this code..

在设置数据库密码时试试这个代码..

//create file 
SQLite.SQLiteConnection.CreateFile("c:\mydatabase file.db3")
Dim cn As New SQLite.SQLiteConnection
//set password
cn.ChangePassword("paxword")
//remove password
cn.ChangePassword("")

Remove the encryption first ..

首先删除加密..

回答by AnarchistGeek

you can provide password via connection string;

您可以通过连接字符串提供密码;

from ConnectionStrings.com

来自ConnectionStrings.com

Data Source=filename;Version=3;Password=myPassword;

数据源=文件名;版本=3;密码=我的密码;

Also, have a look at his link

另外,看看他的链接

hope it helps

希望能帮助到你

回答by Douglas Flores

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

很久以前,但这是我的解决方案,它使用连接字符串和密码与 LITEDB 一起使用。但请记住,您需要先在 db 中设置密码。您可以使用此工具 (LITE DB EXPLORER) 来创建和管理您的数据库。 https://github.com/JosefNemec/LiteDbExplorer

In App Config Add your connection string like this example:

在 App Config 中添加您的连接字符串,如下例所示:

<?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:

在 C# 代码后面:

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;
        }
    }