views:

209

answers:

4

Hi There...

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

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.

Any help would be greatly appreciated:)

A: 

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.AppSettings["PROJECT_NAME"];
Chris Kimpton
A: 

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

Chris Kimpton
A: 

Cool thanks Chris... I'll give the first option a bash. What do you mean with the second option though??? Something like creating a function to input the connection string???

+1  A: 

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

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

Then setup your app.config like so:

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