connectionStrings 項目 (ASP.NET 設定結構描述)

針對 ASP.NET 應用程式和功能,指定資料庫連接字串集合 (做為名稱/值的配對)。

這個項目是 .NET Framework 2.0 版中新增的項目。

<connectionStrings > 
   <add />
   <clear />
   <remove />
</connectionStrings>

屬性和項目

下列小節描述這個項目的屬性、子項目和父項目。

屬性

無,除了由所有區段項目所繼承的屬性以外。 如需詳細資訊,請參閱Section 項目繼承的一般屬性。 在 ASP.NET 應用程式中,您可以在執行階段將替代組態檔的名稱指派給 ConfigSource 屬性。 在這種情況下,包含在 Web.config 檔中的預設連接字串資訊就會覆寫檔案的內容。 當替代檔案不存在,且它是在執行階段建立的,也會發生這個問題。 如果 Web.config 不包含任何連接字串資訊,會將空的 connectionStrings 區段加入至替代檔案。

子項目

項目

描述

Add

將連接字串 (Connection String) 當做名稱/值組,加入至連接字串集合。

Clear

移除繼承之連接字串的所有參考,只允許由目前的 add 項目所加入的連接字串。

remove

移除連接字串集合中繼承連接字串的參考。

父項目

項目

描述

configuration

指定 Common Language Runtime 和 .NET Framework 應用程式所使用之每個組態檔中需要的根項目 (Root Element)。

System.web

指定組態檔中 ASP.NET 組態設定的根項目 (Root Element),並包含會設定 ASP.NET Web 應用程式以及控制這些應用程式之行為的組態項目。

備註

connectionStrings 項目會針對 ASP.NET 應用程式和功能,指定資料庫連接字串集合 (做為名稱/值的配對)。

在舊版 ASP.NET 中,連接字串儲存在 appSettings 中。 在 ASP.NET 2.0 中,功能 (例如工作階段、成員資格、個人化和角色管理員) 會依賴 connectionStrings 項目中儲存的連接字串。 您還可以使用 connectionStrings 項目,儲存您自己應用程式的連接字串。

預設的組態

會繼承父組態檔中包含的連接字串,除非子組態檔中使用 clear 項目。 下列預設 connectionStrings 項目是在 Machine.config 檔案中設定。

您可以程式設計方式存取 connectionStrings 項目。 如需詳細資訊,請參閱 ConnectionStrings 範例。

<connectionStrings>
   <add 
      name="LocalSqlServer" 
      connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" 
      providerName="System.Data.SqlClient"
   />
</connectionStrings>

範例

下列程式碼範例將示範如何設定兩個連接字串。 第一個連接字串可以由 ASP.NET 應用程式使用,第二個連接字串由 sqlCacheDependency 項目使用,此項目會在稍後設定。

<configuration>
<!-- Other configuration settings -->

<connectionStrings>

  <add name="Sales" 
       providerName="System.Data.SqlClient"
       connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" />

  <add name="NorthWind" 
       providerName="System.Data.SqlClient" 
       connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />

</connectionStrings>

<cache>
  <sqlCacheDependency enabled="true" pollTime="60000">
    <databases>
      <add name="Northwind" />
    </databases>
  </sqlCacheDependency>
</cache>

</configuration>

項目資訊

組態區段處理常式

ConnectionStringsSection

組態成員

ConnectionStringSettingsCollection

ConnectionStrings

ConnectionStrings

ConnectionStrings

可設定的位置

Machine.config

根層次的 Web.config

應用程式層級的 Web.config

虛擬或實體目錄層級的 Web.config

需求

Microsoft Internet Information Services (IIS) 5.0、5.1 或 6.0 版

.NET Framework 2.0 版

Microsoft Visual Studio 2003 或 Visual Studio 2005

請參閱

工作

HOW TO:鎖定 ASP.NET 組態設定

參考

configuration 項目 (一般設定結構描述)

connectionStrings 的 add 項目 (ASP.NET 設定結構描述)

connectionStrings 的 clear 項目 (ASP.NET 設定結構描述)

connectionStrings 的 remove 項目 (ASP.NET 設定結構描述)

概念

ASP.NET 組態概觀

保護 ASP.NET 組態

ASP.NET 資料存取概觀

建立 SQL Server 的應用程式服務資料庫

設定資料存取的安全性

其他資源

ASP.NET 組態設定