Fetching connection string from appconfig file in c#

前端 未结 10 1007
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 07:39

I have the following connection string declared in my app.config file:

  
    

        
10条回答
  •  情话喂你
    2020-12-19 08:03

    For a non-web project, and with app.config set up as in the OP here's what I usually do, since the config file changes names when the app is compiled (to yourapp.exe.config):

        public static Configuration ExeConfig()
        {
            Assembly service = Assembly.GetAssembly(typeof(YourClass));
            return ConfigurationManager.OpenExeConfiguration(service.Location);
        }
    

    Then to reference the s'th connection string:

    ExeConfig().ConnectionStrings.ConnectionStrings[s].ConnectionString
    

提交回复
热议问题