在web.config中放置连接字符串的位置
本文关键字:连接 字符串 位置 web config | 更新日期: 2023-09-27 18:30:03
我的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>
当我在<configuration>
下面添加连接字符串时,我会得到一个错误,说只允许使用一个<configSections>
元素。我应该把连接线放在哪里?
只需将其放置在</configSections>
f.e.之后的<configuration>
内
<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>
...
连接字符串可以添加到配置中的任何位置,这样它就应该是配置的子级
建议将其放置在所有标记之后,以便在将来需要更改时保持可见。
<configuration>
<connectionStrings>
<add name="defaultConn"
connectionString="Server=SERVER; Database=DbName; User Id=userid; password= password"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
点击此处阅读更多信息。
您可以在配置后立即添加,只需尝试以下配置
<configuration>
<connectionStrings>
<add name="SQLDbConnection"
connectionString="Server=SQlServerName; Database=YouDatabaseName; User Id=userid; password= password"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
连接字符串位于<connectionStrings>
元素内部。放置<connectionStrings>
的传统位置似乎就在<appSettings>
之前,但它的精确位置应该无关紧要。