[ASP.NET][web.config]如何將資料庫連線設定集中在另一個config檔?;web.config可以吃別的*.config檔嗎?;多個*.config檔

  • 1677
  • 0

.

網站根目錄下,web.config檔案的內容:

其中的 connectionStrings 這小節設定,我們將configSource屬性值設為"ConnStr.config"。指定關於資料庫連線的設定都集中在ConnStr.config檔案裡:

<?xml version="1.0"?>
<configuration>
  <connectionStrings configSource="ConnStr.config"/>
<!--
    .....其它設定
-->
</configuration>

ConnStr.config檔案內容:

(此範例裡面配置了兩個連線,一個連線名稱叫做"DB01",連到SQL Server的資料庫;另一個連線名稱叫做"DB02",連到Informix的資料庫)

<connectionStrings>
    <add name="DB01" providerName="System.Data.SqlClient" connectionString="Data Source=****;Initial Catalog=****; user id=****;password=****;"/>
    <add name="DB02" providerName="IBM.Data.Informix" connectionString="Database=****;Host=192.168.***.***;Server=****;Service=****;Protocol=onsoctcp;UID=****;Password=****;DB_LOCALE=zh_tw.big5;CLIENT_LOCALE=zh_tw.big5;delimident=n;Connection Lifetime=600;" />
</connectionStrings>