配置文件-集中应用程序设置中的值并在连接字符串中使用



不确定这是否可能,但认为询问任何一种方式都没有坏处。

有一种情况,在应用程序设置<add key="database" value="dbName" />中声明变量,然后在连接字符串元素中"理论上"引用(如<add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=%database%;Persist Security Info=True;Pooling=False;Integrated Security=yes" providerName="System.Data.SqlClient"/> )会容易得多

从语法上讲,我确信%database%不会很好,但只是为了抵消我放置它的位置。

目前,我有5个连接字符串,它们使用相同的数据库,对每个数据库的模式访问不同,对对象组件的访问角色不同。

如果我所要做的只是简单地编辑应用程序设置密钥,并通过代理将其通过引用与所有其他密钥传播,那么部署也会更容易。

我相信你们中的一些人会说,只需将连接字符串设置为应用程序本身,然后对关键字进行查找和替换,但我正在寻找不会在App_Load或其他方面进行轻微手动修改的解决方案。

同样,这是一个令人头脑风暴的想法,并想把它抛给其他可能比我"知识渊博"的人。

您可以将Transforms用于配置文件。您也可以参考Oleg-Sych-config转换。

web.config

<connectionStrings>
    <clear />
    <add name="xyz" providerName="System.Data.SqlClient" connectionString="Server=(local);Database=fbi;User Id=test;Password=test" />
  </connectionStrings>

web.release.config

<connectionStrings>
      <add name="xyz" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>

最新更新