Where to place the connection string in web.config

My web.config looks like this:

<configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5.1" /> <httpRuntime targetFramework="4.5.1" /> </system.web> <runtime> 

When I add the connection string just below the <configuration> , I get an error that only one <configSections> element is <configSections> . Where should I put the connection string?

+6
source share
5 answers

Just place it inside the <configuration> right after </configSections> fe

 <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="DefaultConnection" connectionString="blablabla" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5.1" /> <httpRuntime targetFramework="4.5.1" /> </system.web> ... 
+9
source
 <connectionStrings> <add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" /> </connectionStrings> 

More details here and here .

+2
source

Connection strings can be added anywhere in the configuration so that it is a child of the configuration.
It is recommended that it be placed after all tags so that it remains visible if you need to change it in the future.

  <configuration> <connectionStrings> <add name="defaultConn" connectionString="Server=SERVER; Database=DbName; User Id=userid; password= password" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> 
+2
source

You can add immediately after configuration, just try to configure

 <configuration> <connectionStrings> <add name="SQLDbConnection" connectionString="Server=SQlServerName; Database=YouDatabaseName; User Id=userid; password= password" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> 
+1
source

Connection strings are included in the <connectionStrings> element. The traditional location for <connectionStrings> seems immediately before <appSettings> , but its exact location does not matter.

+1
source

Source: https://habr.com/ru/post/978076/


All Articles