简体   繁体   English

如何设置数据库文件到web.config的相对路径?

[英]How to set relative path to database file into web.config?

I'm trying to set relative path of database file into my web.config . 我试图将数据库文件的相对路径设置到我的web.config I have two projects: DAL (class library), WebApi application. 我有两个项目:DAL(类库),WebApi应用程序。 My database store in App_Date folder in DAL. 我的数据库存储在DAL的App_Date文件夹中。 I want to set the connection string to web.config of WebApi project. 我想将连接字符串设置为WebApi项目的web.config

<add key="connectionString" value="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|Data Directory|\DAL\App_Data\Taskstorage.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>

And this is another example: 这是另一个示例:

<add key="connectionString" value="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=~\..\DAL\App_Data\Taskstorage.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>

None of them is working. 他们都没有工作。 Could you tell me how to fix this problem? 你能告诉我如何解决这个问题吗?

try the following code to read your config file. 请尝试以下代码来读取您的配置文件。

using System.Configuration;
string t=ConfigurationManager.AppSettings["connectionString"].ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM