Partager via


connectionStrings Element (ASP.NET Settings Schema) 

Specifies a collection of database connection strings, as name/value pairs, for ASP.NET applications and features.

This element is new in the .NET Framework version 2.0.

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

Attributes and Elements

The following sections describe attributes, child elements, and parent elements for this element.

Attributes

None, except the attributes that are inherited by all section elements. For more information, see General Attributes Inherited by Section Elements.

Child Elements

Element Description

add

Adds a connection string as a name/value pair to the collection of connection strings.

clear

Removes all references to inherited connection strings, allowing only the the connection strings that are added by the current add element.

remove

Removes a reference to an inherited connection string from the collection of connections strings.

Parent Elements

Element Description

configuration

Specifies the required root element in every configuration file that is used by the common language runtime and the .NET Framework applications.

system.web

Specifies the root element for the ASP.NET configuration settings in a configuration file and contains configuration elements that configure ASP.NET Web applications and control how the applications behave.

Remarks

The connectionStrings element specifies a collection of database connection strings, as name/value pairs, for ASP.NET applications and features.

In previous versions of ASP.NET, connection strings were stored in the appSettings. In ASP.NET 2.0, features, such as Session, Membership, Personalization, and Role Manager, rely on connection strings that are stored in the connectionStrings element. You can also use the connectionStrings element to store connection strings for your own applications.

Default Configuration

Connection strings that are contained in a parent configuration file are inherited, unless the clear element is used in the child configuration file. The following default connectionStrings element is configured in the Machine.config file.

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

Example

The following code example demonstrates how to configure two connection strings. The first connection string can be used by an ASP.NET application and the second connection string is used by the sqlCacheDependency element, which is configured later.

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

<connectionStrings>

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

  <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>

Element Information

Configuration section handler

ConnectionStringsSection

Configuration member

ConnectionStringSettingsCollection

ConnectionStrings

ConnectionStrings

ConnectionStrings

Configurable locations

Machine.config

Root-level Web.config

Application-level Web.config

Virtual or physical directory–level Web.config

Requirements

Microsoft Internet Information Services (IIS) version 5.0, 5.1, or 6.0

The .NET Framework version 2.0

Microsoft Visual Studio 2003 or Visual Studio 2005

See Also

Tasks

How to: Lock ASP.NET Configuration Settings

Reference

configuration Element (General Settings Schema)
add Element for connectionStrings (ASP.NET Settings Schema)
clear Element for connectionStrings (ASP.NET Settings Schema)
remove Element for connectionStrings (ASP.NET Settings Schema)

Concepts

ASP.NET Configuration Overview
Securing ASP.NET Configuration
ASP.NET Data Access Overview
Installing the SQL Server Provider Database
Securing Data Access

Other Resources

ASP.NET Configuration Settings