Share via


add Element for connectionStrings (ASP.NET Settings Schema)

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

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

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

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute Description

connectionString

Required String attribute.

Defines the connection string value. The value can be an empty string (""). No checking is done to verify its validity.

The default is an empty string ("").

name

Required String attribute.

Defines the key to use to access the connection string value. This attribute overrides any inherited key with the same name.

The default is an empty string ("").

providerName

Optional String attribute.

The name of the ADO.NET provider to use to access the underlying data store.

The default is "System.Data.SqlClient".

Inherited attributes

Optional attributes.

Attributes inherited by all section elements.

For more information, see General Attributes Inherited by Section Elements.

Child Elements

None.

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.

connectionStrings

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

Remarks

The add element adds a connection string as a name/value pair to the collection of connection strings in the connectionStrings element.

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

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 element 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 a connection string for an application that uses the Northwind database.

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

Element Information

Configuration section handler

ConnectionStringsSection

Configuration member

Add

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

clear Element for connectionStrings (ASP.NET Settings Schema)
configuration Element (General 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
Securing Data Access
ASP.NET Configuration Scenarios