SQL配置文件显示服务器未发现异常

本文关键字:未发现 异常 服务器 显示 配置文件 SQL | 更新日期: 2023-09-27 18:10:48

我试图在我的程序中使用配置文件。从命令行使用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)); }
      }
    ....
}

SQL配置文件显示服务器未发现异常

证明代码的Membership.GetUser()部分仍然试图使用在机器中找到的LocalSqlServer连接字符串。配置文件,而不是我在web.config. config中提供的数据库连接字符串。在我的connectionString配置中添加<clear />帮助我解决了这个问题。

<connectionStrings>
<clear />
    <add name="..." connectionString="Data Source=...;Initial Catalog=...;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>