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


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


One option is for the connection class to use the ConfigurationManager to get the name from the App.Config file, but that still means setting the name in it. Something like



ConfigurationManager.AppSettings["PROJECT_NAME"];

      

0


source


Or edit your shared code so there is no project name ...

0


source







All Articles