Dynamically assigning a project property name to a variable in a class
I created a class to dynamically combine SQL function statements within a project. I found this class very useful and would like to include in future projects
namespace connectionClass
{
public class connClass
{
NpgsqlConnection conn = new NpgsqlConnection(projectName.Properties.Settings.Default.ConnString);
}
}
I want to be able to dynamically enter the project name without having to do it myself for every other class! the connection string will be defined in the properties settings in VS.
Any help would be greatly appreciated :)
+1
nick1
source
to share
3 answers
Or, just use the ConnectionString property of the Configuration Manager:
String connStr = ConfigurationManager.ConnectionStrings["DefaultConnStr"].ConnectionString;
Then set up your app.config like this:
<configuration>
<connectionStrings>
<add name="DefaultConnStr" connectionString="Data Source=127.0.0.1..."/>
</connectionStrings>
</configuration>
+1
source to share