【问题标题】:Where to place connection string in web.config在 web.config 中放置连接字符串的位置
【发布时间】:2014-11-12 08:48:27
【问题描述】:

我的 web.config 看起来像这样:

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

当我在&lt;configuration&gt; 下方添加连接字符串时,我收到一条错误消息,指出只允许使用一个&lt;configSections&gt; 元素。我应该把我的连接字符串放在哪里?

【问题讨论】:

    标签: c# asp.net-mvc connection-string


    【解决方案1】:

    只需将其放在&lt;configuration&gt; 中,紧跟在&lt;/configSections&gt; 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】:

      可以在配置中的任何位置添加连接字符串,使其成为配置的子项。
      建议将它放在所有标签之后,以便将来需要更改时它仍然可见。

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

      【讨论】:

        【解决方案3】:
        <connectionStrings>
              <add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
        </connectionStrings>
        

        阅读更多herehere

        【讨论】:

          【解决方案4】:

          你可以在配置后立即添加,只需尝试以下配置

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

          【讨论】:

            【解决方案5】:

            连接字符串位于&lt;connectionStrings&gt; 元素中。放置&lt;connectionStrings&gt; 的传统位置似乎就在&lt;appSettings&gt; 之前,但它的确切位置并不重要。

            【讨论】:

              猜你喜欢
              • 2015-08-30
              • 1970-01-01
              • 1970-01-01
              • 2021-08-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-10-23
              相关资源
              最近更新 更多