在ASP.NET中从connectionStrings动态读取configSource的值

本文关键字:读取 configSource 的值 动态 connectionStrings ASP NET 中从 | 更新日期: 2023-09-27 18:23:54

在开发过程中,根据需要,我手动更改configSource以指向本地SQL Server或SQL Azure服务器。我想让我的代码找出configSource的值,这样我的代码就知道正在使用什么数据库服务器。

使用C#,如何获得当前的configSource值?

请注意,这与获取Windows应用程序的configSource不同!这种方式在ASP.NET中不起作用!

在ASP.NET中从connectionStrings动态读取configSource的值

以下代码将从web.config中读取connectionStrings的配置部分,并获取ConfigSource属性:

ConnectionStringsSection connectionStringsSection = 
      System.Web.Configuration.WebConfigurationManager
     .GetSection("connectionStrings", "/Web.config") as ConnectionStringsSection;
string configSource = connectionStringsSection.SectionInformation.ConfigSource;