Managing A Debug and Release Connection String

前端 未结 9 1303
北海茫月
北海茫月 2021-01-30 18:41

What is a good approach to managing a debug and release connection string in a .NET / SQLServer application?

I have two SQL Servers, a production and a build/debug and I

相关标签:
9条回答
  • 2021-01-30 19:39

    I use a combination of Sameh's and Obalix's method in .net 3.5.

        public static class DataConnection
    {
    #if LOCALDEV
        public const string Env = "Debug";
    #endif
    #if STAGING
        public const string Env="Staging";
    #endif
    #if RELEASE
        public const string Env="Release";
    #endif
        private static ConnectionStringSettingsCollection _connections;
        static DataConnection()
        {
            _connections = ConfigurationManager.ConnectionStrings;
    
        }
    
        public static string BoloConnectionString
        {
            get
            {
                return _connections["DB1."+Env].ConnectionString;
            }
        }
        public static string AOAConnectionString
        {
            get
            {
                return _connections["DB2."+Env].ConnectionString;
            }
        }
        public static string DocVueConnectionString
        {
            get
            {
                return _connections["DB3."+Env].ConnectionString;
            }
        }
    }
    

    Then in my project properties, I define the right conditional compilation symbols. This way I don't have to keep my connection strings hard coded like Sameh's, but the code only looks for the string based on how it was built. This lets me have (if I need to) one config file for all the builds, but in reality I don't deploy the config files in my build process. Although the conditional app.Relase.config stuff for .net 4 looks like the right way to go in the future.

    0 讨论(0)
  • 2021-01-30 19:41

    Well perhaps this is a bit outdated, but the ODBC DSN solves this problem quite well -- I still use -- with rigour -- DNS settings to differentiate between production and debug environments.

    p.s., I am expecting loads of down-votes, perhaps this will be a gauge of what people think of a level of indirection for database identifiers.

    0 讨论(0)
  • 2021-01-30 19:43

    When you open a web project you'll get 2 extra files of the Web.Config out of the box - Web.Debug.config & Web.Release.config.

    1.Add your desired connection string to those files with XSLT attributes xdt:Transform="SetAttributes" xdt:Locator="Match(name)"

    <connectionStrings>
      <add name="myConnectionString" connectionString="myConnectionString" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
    </connectionStrings>"
    

    2.Edit your csproj and add a TransformXml target:

    <Target Name="TransformActiveConfiguration" Condition="Exists('$(ProjectDir)/Web.$(Configuration).config')" BeforeTargets="Compile" >
        <TransformXml Source="$(ProjectDir)/Web.Config" Transform="$(ProjectDir)/Web.$(Configuration).config" Destination="$(TargetDir)/Web.config" />
    </Target>
    

    The second step will make the transform on each build (according to your active configuration) and not only on publish, giving you a better debug experience. I learnt it from this post.

    0 讨论(0)
提交回复
热议问题