Share via


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

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

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

<add 
   name="connection string name"
   connectionString="connection string"
   providerName="ADO.NET data provider" 
/>

屬性和項目

下列各節將說明屬性、子項目和父項目。

屬性

屬性

說明

connectionString

必要的 String 屬性。

定義連接字串值。 值可以為空字串 ("")。 不進行檢查以驗證其有效性。

預設為空字串 ("")。

name

必要 String 屬性。

定義用於存取連接字串值的金鑰。 這個屬性會覆寫所有相同名稱之繼承的金鑰。

預設為空字串 ("")。

providerName

選擇性 String 屬性。

用於存取基礎資料存放區的 ADO.NET 提供者名稱。

預設值為 "System.Data.SqlClient"。

繼承的屬性

選擇性屬性。

所有區段項目繼承的屬性。

如需詳細資訊,請參閱 Section 項目繼承的一般屬性

子項目

無。

父項目

項目

描述

configuration

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

system.web

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

connectionStrings

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

備註

add 項目將連接字串當做名稱/值組,加入至 connectionStrings 項目中的連接字串集合。

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

預設組態

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

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

範例

下列程式碼範例將示範如何設定使用 Northwind 資料庫之應用程式的連接字串。

<configuration>
  <connectionStrings>
    <add name="Northwind"
         connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" 
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

項目資訊

組態區段處理常式

ConnectionStringsSection

組態成員

Add(ConnectionStringSettings)

可設定的位置

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 組態設定

參考

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

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

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

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

概念

ASP.NET 組態概觀

保護 ASP.NET 組態

ASP.NET 資料存取概觀

設定資料存取的安全性

ASP.NET 組態案例