SetPropertyValue期间CreateUserWizard出错:无法连接到SQL Server数据库

本文关键字:连接 SQL Server 数据库 期间 CreateUserWizard 出错 SetPropertyValue | 更新日期: 2023-09-27 17:59:11

我有一个ASP.NET应用程序,它通常可以连接数据库,可以使用CreateUserWizard创建用户,但在开始使用配置文件时,我在用户配置文件更新过程中遇到了以下异常:

[SqlException(0x80131904):用户"My PC''user"登录失败。]
[HttpException(0x80004005):无法连接到SQL Server数据库。]

我确信以下项目:

  1. 连接字符串正常,应用程序可以访问数据库
  2. 如果i不想更新用户的配置文件,则在数据库中使用CreateUserWizard创建用户,不会出现错误

只有当我想调用this.SetPropertyValue("FirstName", value);时,一切都会出错,并显示以下错误:

SQLExpress数据库文件自动创建错误:

连接字符串使用应用程序的App_Data目录中的数据库位置指定本地Sql Server Express实例。提供程序尝试自动创建应用程序服务数据库,因为提供程序确定该数据库不存在。以下配置要求对于成功检查应用程序服务数据库的存在并自动创建应用程序服务数据库是必要的:

如果应用程序在Windows 7或Windows Server 2008R2上运行,则需要执行特殊的配置步骤才能自动创建提供程序数据库。更多信息请访问:http://go.microsoft.com/fwlink/?LinkId=160102.如果应用程序的App_Data目录不存在,则web服务器帐户必须具有对应用程序目录的读写访问权限。这是必要的,因为如果App_Data目录不存在,web服务器帐户将自动创建该目录。

如果应用程序的App_Data目录已经存在,则web服务器帐户只需要对应用程序的App_Data目录进行读写访问。这是必要的,因为web服务器帐户将尝试验证Sql server Express数据库是否已存在于应用程序的App_Data目录中。从web服务器帐户吊销对App_Data目录的读取访问权限将使提供程序无法正确确定Sql server Express数据库是否已存在。当提供程序尝试创建已存在数据库的副本时,这将导致错误。需要写入访问权限,因为在创建新数据库时会使用web服务器帐户的凭据。

必须在计算机上安装Sql Server Express
web服务器帐户的进程标识必须具有本地用户配置文件。有关如何为计算机帐户和域帐户创建本地用户配置文件的详细信息,请参阅自述文档。

这里是web.config:

<configuration>
  <connectionStrings>
    <add name="MUQ_SMSConnectionString" 
         connectionString="Data Source=MY-PC'SQLEXPRESS12SP1;Initial Catalog=MyDB;" 
         providerName="System.Data.SqlClient;Integrated Security=True"/>
  </connectionStrings>
  ............
</configuration>

以下是配置文件更新方法:

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    ProfileCommon userProfile = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);
    userProfile.FirstName = textBoxFirstName.Text.Trim();
    userProfile.LastName = textBoxLastName.Text.Trim();
    userProfile.MobileNumber = textBoxUserName.Text.Trim();
    if (radioButtonMaleGender.Checked == true)
        userProfile.Gender = MALE_GENDER_TAG;
    else if (radioButtonFemaleGender.Checked == true)
        userProfile.Gender = FEMALE_GENDER_TAG;
    else
        userProfile.Gender = UNKNOW_GENDER_TAG;
    userProfile.Save();
}

以下是配置文件的定义和默认提供商的覆盖:

<membership defaultProvider="MySqlProvider" >
      <providers>
        <add name="MySqlProvider"
             type="System.Web.Security.SqlMembershipProvider"
             applicationName="/"
             connectionStringName="MUQ_SMSConnectionString"
              requiresUniqueEmail="False"
             requiresQuestionAndAnswer="false" />
      </providers>
    </membership>
    <anonymousIdentification enabled="true" />
    <profile enabled="true">
      <properties>
        <add name="MobileNumber" type="string" allowAnonymous="true"/>
        <add name="FirstName" type="string" allowAnonymous="true"/>
        <add name="LastName" type="string" allowAnonymous="true"/>
        <add name="Gender" type="string" allowAnonymous="true"/>
      </properties>
    </profile>
    <roleManager enabled="true"/>

注意:我有另一个问题,我怀疑它可能与这个错误有关。当我想在应用程序的管理面板中编辑应用程序的安全选项时,SQL Server会崩溃并报告窗口中的错误。

目前,我在Windows 8.1上使用SQL Server 2012SP1 Express。我安装了SQL Server 2008 R2 Express,然后安装了2012 Express,但没有任何变化。

SetPropertyValue期间CreateUserWizard出错:无法连接到SQL Server数据库

如果使用AspDotNetProfile提供程序而没有在web.config文件中提供提供程序配置,则使用的是machine.config文件中的提供程序配置。以下是与.NET 4.0一起分发的默认machine.config文件的示例:

    <profile>
        <providers>
            <add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
    </profile>

此定义中引用的连接字符串也可以在machine.config:中找到

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.'SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

从您的问题中可以看出,您没有覆盖web.config中的默认配置文件定义。因此,配置文件提供程序正在本地计算机上查找名为SQLEXPRESS的SQL Server Express实例。原因是必须是Express实例,因为只有Express版本才支持用户实例(请参阅文档中的AttachDBFilename)。

因此,您有两个选择:1.覆盖默认提供程序配置。这可以通过删除默认配置并重新添加具有相同名称的配置来完成。2.在web服务器上使用默认实例名称设置SQL Server Express实例。