Where to place connection string in web.config

时光怂恿深爱的人放手 提交于 2019-12-01 03:52:25

Just place it inside <configuration> right after </configSections> f.e.

<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>
    ...
<connectionStrings>
      <add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

Read more here and here.

Connection strings can be added any where in configuration in such a way it should be a child of configuration.
Its recommended that it should be placed after all tags so it remains visible if you need to change it in future.

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

You can add right after configuration, Just try following config

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

Connection strings go inside a <connectionStrings> element. The traditional place to put <connectionStrings> seems to be immediately before <appSettings> but its precise location shouldn't matter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!