0

I'm trying to set relative path of database file into my web.config. I have two projects: DAL (class library), WebApi application. My database store in App_Date folder in DAL. I want to set the connection string to web.config of WebApi project.

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

Joseph Katzman
  • 1,959
  • 6
  • 21
  • 47

1 Answers1

0

try the following code to read your config file.

using System.Configuration;
string t=ConfigurationManager.AppSettings["connectionString"].ToString();
imsome1
  • 1,182
  • 4
  • 22
  • 37
  • I did before asking my question. I get an error trying to connect to my database. This is what I get from `AppSetting`: `"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=~\\..\\DAL\\App_Data\\Taskstorage.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"` – Joseph Katzman Feb 08 '17 at 09:30
  • please check this post http://stackoverflow.com/questions/3500829/sql-express-connection-string-mdf-file-location-relative-to-application-locatio – imsome1 Feb 08 '17 at 09:34
  • Suddenly it doesn't suit for me. My database file is situated into another project of common solution. I need something like this one: `AttachDbFilename=|DataDirectory|\..\DAL\App_Data\Taskstorage.mdf;` – Joseph Katzman Feb 08 '17 at 09:58