我正在尝试在程序中使用“个人档案”。从命令行使用aspnet_regsql.exe创建必要的表可以正常工作。但是,抛出System.Web.HttpException异常,表示我尝试运行该应用程序时无法连接到数据库。这很奇怪,因为我已连接到VS 2013中服务器浏览器上的数据库。这是应用程序网站上的错误消息。
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。(提供者:SQL网络接口,错误:26-指定服务器/实例时出错)
这是我的Web配置文件中的相关代码。
<profile defaultProvider="SqlProvider" inherits="[Namespace].AccountProfile">
<providers>
<clear/>
<add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="[stringname]"/>
</providers>
<properties>
<add name="Rows" type="System.String"/>
<add name="Area" type="System.String"/>
</properties>
</profile>
这是我的应用程序中引发异常的代码
AccountProfile.cs
public class AccountProfile : ProfileBase
{
static public AccountProfile CurrentUser
{
get { return (AccountProfile)(ProfileBase.Create(Membership.GetUser().UserName)); }
}
....
}
原来,代码的Membership.GetUser()部分仍在尝试使用machine.config文件中找到的LocalSqlServer连接字符串,而不是我在web.config中提供的数据库连接字符串。<clear />
在我的connectionString配置中添加帮助我弄清楚了这一点。
<connectionStrings>
<clear />
<add name="..." connectionString="Data Source=...;Initial Catalog=...;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句