Where to place connection string in web.config

六月ゝ 毕业季﹏ 提交于 2019-12-19 06:05:17

问题


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 my connection string just below <configuration> I get an error saying that only one <configSections> element is allowed. Where should I put my connection string?


回答1:


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



回答2:


<connectionStrings>
      <add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

Read more here and here.




回答3:


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>



回答4:


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>



回答5:


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.



来源:https://stackoverflow.com/questions/26882704/where-to-place-connection-string-in-web-config

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