动态地将项目属性名称分配给类中的变量

时间:2021-01-07 21:16:12

I have created a class to dynamically put together SQL function statements within a project. I have found this class to be pretty useful and would like to incorporate into future projects

我创建了一个类来动态地将SQL函数语句组合在一个项目中。我发现这个课程非常有用,并且想要融入未来的项目中

namespace connectionClass

{

 public class connClass

 {      

     NpgsqlConnection conn = new NpgsqlConnection(projectName.Properties.Settings.Default.ConnString);  

 }

}

I want to be able to dynamically input the project name without having to do it myself for every different class! the connection string will be defined within the properties settings in VS.

我希望能够动态输入项目名称,而不必为每个不同的类自己做!连接字符串将在VS的属性设置中定义。

Any help would be greatly appreciated:)

任何帮助将不胜感激:)

3 个解决方案

#1


1  

Or simply make use of the Configuration Manager Connection Strings property:

或者只是使用Configuration Manager连接字符串属性:

String connStr = ConfigurationManager.ConnectionStrings["DefaultConnStr"].ConnectionString;

Then setup your app.config like so:

然后像这样设置你的app.config:

<configuration>
    <connectionStrings>
         <add name="DefaultConnStr" connectionString="Data Source=127.0.0.1..."/>
    </connectionStrings>
</configuration>

#2


0  

One option is to have the connection class use the ConfigurationManager to get the name from the App.Config file - but this still means setting the name in there. Something like

一种选择是让连接类使用ConfigurationManager从App.Config文件中获取名称 - 但这仍然意味着在那里设置名称。就像是

ConfigurationManager.AppSettings["PROJECT_NAME"];

#3


0  

Or refactor your common code to not need a project name...

或者重构您的公共代码,不需要项目名称......

#1


1  

Or simply make use of the Configuration Manager Connection Strings property:

或者只是使用Configuration Manager连接字符串属性:

String connStr = ConfigurationManager.ConnectionStrings["DefaultConnStr"].ConnectionString;

Then setup your app.config like so:

然后像这样设置你的app.config:

<configuration>
    <connectionStrings>
         <add name="DefaultConnStr" connectionString="Data Source=127.0.0.1..."/>
    </connectionStrings>
</configuration>

#2


0  

One option is to have the connection class use the ConfigurationManager to get the name from the App.Config file - but this still means setting the name in there. Something like

一种选择是让连接类使用ConfigurationManager从App.Config文件中获取名称 - 但这仍然意味着在那里设置名称。就像是

ConfigurationManager.AppSettings["PROJECT_NAME"];

#3


0  

Or refactor your common code to not need a project name...

或者重构您的公共代码,不需要项目名称......