0

我有一个 asp .net 2.0 web 应用程序。它在我的本地机器上运行良好。我将该网站发布给了我的托管服务提供商,但它给出了以下错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我的 web.config 文件中的设置如下:

 <membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
  </providers>
</membership>

<add name="classifiedsConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CLASSIFIEDSDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

所有 mdf ldf 文件都上传到主机。我在 stackoverflow 上检查了许多类似的问题,但解决方案对我没有帮助。以这种方式使用 sqlexpress 有错吗?我该如何解决这个问题?

任何帮助将不胜感激。谢谢,谢谢。

编辑:

web.config 文件中没有 LocalSQLServer 连接字符串。但要确保我尝试添加此连接字符串

<add name="LocalSqlServer" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

但结果没有改变。

编辑2:

我更新了提供者的连接字符串,现在我的 web.config 文件如下:

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <location path="Admin">
    <system.web>
      <authorization>
        <allow roles="Administrators"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="PostAd.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
  <location path="EditPhotos.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
  <location path="MyAds.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
  <location path="MyProfile.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
  <system.web>
    <customErrors mode="Off"/>
    <pages styleSheetTheme="Red"/>
    <authentication mode="Forms"/>
    <membership>
      <providers>
      <clear/>
      <add name="AspNetSqlMembershipProvider"
           type="System.Web.Security.SqlMembershipProvider, System.Web,     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           connectionStringName="membershipConnection"
           enablePasswordRetrieval="false"
           enablePasswordReset="true"
           requiresQuestionAndAnswer="true"
           applicationName="/"
           requiresUniqueEmail="true"
           passwordFormat="Hashed"
           maxInvalidPasswordAttempts="5"
           minRequiredPasswordLength="7"
           minRequiredNonalphanumericCharacters="1"
           passwordAttemptWindow="10"
           passwordStrengthRegularExpression=""/>
    </providers>
    </membership>
    <profile enabled="true">
      <properties>
        <add name="FirstName" type="System.String"/>
        <add name="LastName" type="System.String"/>
        <add name="MemberId" defaultValue="0" type="System.Int32"/>
        <group name="Core"/>
      </properties>
    </profile>
    <roleManager enabled="true"/>
    <compilation debug="true"/>
    <siteMap defaultProvider="RoleEnabled_AspNetXmlSiteMapProvider" enabled="true">
      <providers>
        <clear/>
        <add name="RoleEnabled_AspNetXmlSiteMapProvider" type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" siteMapFile="web.sitemap" securityTrimmingEnabled="true"/>
      </providers>
    </siteMap>
  </system.web>
  <connectionStrings>
    <add name="classifiedsConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CLASSIFIEDSDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
<add name="membershipConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>      
</configuration>

我在可能的 app_data 文件夹中有 aspnetdb.mdf 和分类文件.mdf 文件,但它仍然无法正常工作。我得到的错误的全文是:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

4

3 回答 3

3

@BrianR 的建议很好(成员资格配置指向一个不存在的连接字符串)。

但是,除此之外,这个问题对于您的托管服务提供商来说实在太具体了,因此与堆栈溢出无关。您应该通过技术支持直接联系您的托管服务提供商。

您看到的错误意味着 IIS/ASP.net 找不到 SqlServer。这将是您的连接字符串或主机 Web 服务器配置错误的结果。这就是为什么这个问题特定于您的主机,不适用于可能通过谷歌找到这个问题的其他用户。不同的主机会有所不同。

  • 一些主机将安装完整的 SqlServer,一些主机将安装 SqlExpress。完整版 SqlServer 不使用相同的默认实例名称安装,可能存在其他配置差异。
  • 出于安全原因或其他系统管理原因,某些主机可能会从默认值(如“SQLEXPRESS”)更改实例名称。(即使安装了SqlExpress,也不一定意味着可以通过'./SQLEXPRESS'访问)
  • 一些主机使用独立的数据库服务器(与您的网站不在同一个 windows 服务器实例上),您可能需要包含机器路径、IP 地址、端口号、安全凭据或其他详细信息才能访问它。如果是这种情况,您的房东会向您提供此信息。有时它会记录在案,一些较小的主机可能会要求您提交支持票(如果有疑问,请获取支持票!)。

由于从一台主机到另一台主机的这些潜在配置差异,您确实需要联系他们。您可能为此目的提供了技术支持!

编辑#1

您对问题的后续编辑并未更改上述信息。您仍然遇到提示您无法连接到 SqlServer 的错误。您仍然需要向您的托管服务提供商验证是否安装了 SqlExpress,您是否具有访问 SqlServer 的正确凭据/权限,并且您的两个数据库文件确实存在于具有正确权限的正确位置。

即使您的数据库架构不正确,您也不会收到此特定错误。所以它必须与SqlServer/SqlExpress的配置、你的数据文件和你的web/config有关。这些设置特定于您的主机,而不是通常可以在 stackoverflow 上回答的内容。

于 2012-07-30T04:39:32.667 回答
2

在您的会员提供者信息中,您需要将 connectionStringName 更改为“classifiedsConnection”。还要确保您的 CLASSIFIEDSDB.mdf 文件实际上位于主机上的 App_Data 文件夹中。

<membership defaultProvider="AspNetSqlMembershipProvider">
   <providers>
      <clear/>
      <add name="AspNetSqlMembershipProvider"
           type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           connectionStringName="classifiedsConnection"
           enablePasswordRetrieval="false"
           enablePasswordReset="true"
           requiresQuestionAndAnswer="true"
           applicationName="/"
           requiresUniqueEmail="true"
           passwordFormat="Hashed"
           maxInvalidPasswordAttempts="5"
           minRequiredPasswordLength="7"
           minRequiredNonalphanumericCharacters="1"
           passwordAttemptWindow="10"
           passwordStrengthRegularExpression=""/>
    </providers>
</membership>
于 2012-07-27T21:00:39.430 回答
2

您的托管服务提供商可能没有安装 SQLExpress。(SQLExpress 不是 dot.net 框架的一部分,它是一个不同的产品。)

于 2012-07-27T21:09:05.193 回答