Managing A Debug and Release Connection String

前端 未结 9 1335
北海茫月
北海茫月 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.

提交回复
热议问题