Question

I have a asp.net webforms project with a database in the App_Data folder referenced by connectionstring:

<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

The solution also contains another project with webservices that need to access the same database. Is it possible to write a relative path to this database in the connnectionstring? - eg. something like this(does not work):

<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|/../../ProjectName/App_Data/database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
Was it helpful?

Solution

Basically |DataDirectory| is an alias for App_Data folder. Since you are not using App_Data in your services project, it would not help you in this situation.

What you can do though is try starting from the project root using ~\:

AttachDbFilename=~\..\ProjectName\App_Data\database.mdf

Also there is always an option to use an absolute path:

AttachDbFilename=d:\Projects\ProjectName\App_Data\database.mdf

And finally there is always an option to build connection string programmatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top