SpirosMesa
SpirosMesa

Reputation: 69

Reference class property in app.config?

Me and a collegue of mine are working on an application that connects to a database, using LINQ(and subsequently DBML files). Each one of us has their own connection string in order to work on the own database like so:

public class DbConfig
{    
   public static string conStr = @"My connection string";
   public static string conStr = @"His connection string";
}

Every time each one of us works on their part of the project, we just comment out the other connection string. My connection is if i can reference any of the two conString properties in the app.config file used by the DBML file. Thank you.

Upvotes: 0

Views: 549

Answers (2)

Not Important
Not Important

Reputation: 804

you could create a personal build configuration ( https://msdn.microsoft.com/en-us/library/kwybya3w.aspx )and define in that configuration some custom tags so that in the code you could have

#if MyBuild
    public static string conStr = @"My connection string";
#endif

#if HisBuild
   public static string conStr = @"His connection string";
#endif

to define your custom

open the project's Property Pages -> Build then modify the Conditional Compilation Constants property after having selected your custom Configuration.

this is how you can define your own symbols and then have the #if #endif that practically include/exclude code based on your current build settings (like writing #define YourSymbol on the top of every page)

another solution could be just to add an external reference to your appsettings, and having that referenced file not be included in your source repository (so that you don't share it) this way any programmer can have its own file configuration

just put in your app.config/web.config a line like

<appSettings configSource="YourCustomConfiguration.config"/>

and then do not add it to teamfoundation or whatever cvs you use

of course the YourCustomConfiguration.config is a file, you can put it wherever you want, just specify a relative path

well, i've said appsettings but you can use configSource for whatever you need, in your case <connectionStrings configSource="YourCustomConnectionStrings.config">

Upvotes: 0

Alberto Monteiro
Alberto Monteiro

Reputation: 6229

You can use you machine name to do this, using the code like that:

public static class ConnectionString
{
    public static string Get
    {
        get
        {
            if(ConfigurationManager.ConnectionStrings.Count == 0)
                throw new Exception("No connection strings");

            var machineConnectionString = ConfigurationManager.ConnectionStrings["ConStringPrefix" + Environment.MachineName];
            var genericConnectionString = ConfigurationManager.ConnectionStrings["DefaultConString"];
            return  machineConnectionString ?? genericConnectionString;
        }
    }
}

And then in you app.config you will have:

<connectionStrings>
    <add name="ConStringPrefix@MyPCName" connectionString="Data Source=.\sqlexpress;Initial Catalog=DatabaseName;Integrated Security=true" providerName="System.Data.SqlClient" />
    <add name="ConStringPrefix@MyFriendPcName" connectionString="Data Source=.\sqlexpress;Initial Catalog=DatabaseName;Integrated Security=true" providerName="System.Data.SqlClient" />
    <add name="nhibernate.conexao" connectionString="Data Source=.\sqlexpress;Initial Catalog=DatabaseName;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>

Upvotes: 2

Related Questions