更改连接字符串,SQLite,Visual Studio后无法打开数据库文件

时间:2021-11-26 12:50:14

I have a ASP.NET MVC solution with 2 projects: DAL and web project. I used to connect to my SQLite database using local file path in connection string of both projects' web.config files:

我有一个包含2个项目的ASP.NET MVC解决方案:DAL和Web项目。我曾经在两个项目的web.config文件的连接字符串中使用本地文件路径连接到我的SQLite数据库:

Web.configs of DAL and Web projects:

DAL和Web项目的Web.configs:

<add name="DBEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;
provider=System.Data.SQLite;
provider connection string='data source=&quot;C:\Users\Admin\Documents\Visual Studio 2013\Projects\Myproj\Myproj.db3&quot;'" 
providerName="System.Data.EntityClient" />

Everything was working fine, and then I needed to change connection string using DataDirectory, so that I could correctly deploy the website. I added the db file into App_Data folder of WEB project and both web.configs changed to:

一切都运行正常,然后我需要使用DataDirectory更改连接字符串,以便我可以正确部署网站。我将db文件添加到WEB项目的App_Data文件夹中,并且web.configs都更改为:

<add name="DBEntities"
connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;
    provider=System.Data.SQLite;
    provider connection string='data source=&quot;|DataDirectory|\Myproj.db3&quot;'" 
    providerName="System.Data.EntityClient" />

Now, I can connect to the db extract data via code, but I cannot open database in server explorer and use designer, the error is: "Unable to open database file".

现在,我可以通过代码连接到db提取数据,但我无法在服务器资源管理器中打开数据库并使用设计器,错误是:“无法打开数据库文件”。

I would appreciate any help!

我将不胜感激任何帮助!

1 个解决方案

#1


0  

I used like this. If you want you can try.

我这样用过。如果你想要你可以尝试。

private readonly static string ConnectionKey = @"C:\Users\Mehmeto\Documents\Visual Studio 2015\Projects\......\bin\myDB.sqlite";

    public static SQLiteConnection GetConnection()
    {

        SQLiteConnection conn = new SQLiteConnection("Data Source=" + ConnectionKey + ";Version=3;", true);
        return conn;
    }

#1


0  

I used like this. If you want you can try.

我这样用过。如果你想要你可以尝试。

private readonly static string ConnectionKey = @"C:\Users\Mehmeto\Documents\Visual Studio 2015\Projects\......\bin\myDB.sqlite";

    public static SQLiteConnection GetConnection()
    {

        SQLiteConnection conn = new SQLiteConnection("Data Source=" + ConnectionKey + ";Version=3;", true);
        return conn;
    }