如何更改ASP.NET配置工具连接字符串

时间:2011-01-10 08:50:45

标签: asp.net string configuration connection

如何更改ASP.NET配置工具的连接字符串名称? (ASP.NET配置工具将使用哪个连接字符串) 我正在学习ASP.NET,无处不在,在书中,我现在正在阅读名为:LocalSqlServer的连接字符串。

我想使用我的本地sql server数据库而不是sql express来存储角色,成员资格和其他数据。

我已经使用aspnet_regsql.exe在我的数据库中创建所需的数据结构。之后我将web.config改为:

<connectionStrings> <remove name="LocalSqlServer"/> <add name="LocalSqlServer" connectionString="Server=(LOCAL); Database=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

但是当我运行ASP.NET配置工具时,它说: “在应用程序配置中找不到连接名称'ApplicationServices',或者连接字符串为空。”

ASP.NET配置工具使用名为“ApplicationServices而非LocalSqlServer。”的连接字符串。

原因我必须将web.config修改为: <connectionStrings> <add name="ApplicationServices" connectionString="Server=(LOCAL); Database=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

一切正常。

我想知道为什么我的网站使用名为的连接字符串:ApplicationServices,所有书籍和在线文档都使用LocalSqlServer?以及如何将其更改为LocalSqlServer?

我有: Windows 7的 Sql Server 2008 R2 Visual Studio 2010 Premium 项目类型是网站

1 个答案:

答案 0 :(得分:15)

在查看web.config文件时,我意外地找到了我的问题答案。

如果覆盖web.config文件中的默认machine.config配置设置,则可以更改ASP.NET配置工具的连接字符串名称。

我从book-s代码存档获取了我的web.config文件,这就是问题所在。

web.config中的

你可以覆盖将用于的成员,配置文件和角色管理器的连接字符串名称。

覆盖会员资格使用:
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="LocalSqlServer"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/"/>
</providers>
</membership>

其中 connectionStringName 是将用于存储成员资格数据的连接字符串的名称。

其他人是:

<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="LocalSqlServer"
applicationName="/"/>
</providers>
</profile>

<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="LocalSqlServer" applicationName="/"
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider"
type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>