I have a c# project ("MainProject") which references another c# project ("UserManager") in my solution. UserManager has a local database (a "Service-based Database", .mdf
).
By adding this database to UserManager, Visual Studio 2015 by default created a connectionstring entry in App.config of UserManager...
<connectionStrings>
<add name="UserManager.Properties.Settings.TPHWDatabaseConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TPHWDatabase.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
... and an entry in Properties.Settings named as the name field of the App.config entry containing the following value:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TPHWDatabase.mdf;Integrated Security=True
In my C# code of the UserManager project I create my SqlConnection object using the following lines:
// Establish conenction to database
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["UserManager.Properties.Settings.TPHWDatabaseConnectionString"].ConnectionString;
sqlConnection.Open();
This all works fine running the UserManager project by itself. The database file (".mdf") is also included in the MainProject's bin folders. When running the MainProject however, "ConfigurationManager.ConnectionStrings
["UserManager.Properties.Settings.TPHWDatabaseConnectionString"]" will be null
and I will get a nullpointer exception for calling ".ConnectionString" on it. Which seems reasonable since I have no Access to "UserManager.Properties.Settings"
and "App.config"
from MainProject.
So my question is: What is a clean way to reference UserManager
( in MainProject) that will keep the database working?
EDIT
I now just copied the connection string as is to the App.config file of my MainProject and set the exact same setting to Properties.Setting. This seems to work and was rather easy. But is is it still the cleanest way to do so?