【问题标题】:Why does only one of these connection strings work when they are actually identical?当它们实际上相同时,为什么只有一个连接字符串起作用?
【发布时间】:2013-03-02 09:54:51
【问题描述】:

我有 2 个相同的连接字符串。一个用于 ASP.NET 成员资格等,另一个用于其他一切。

我可以登录到我的应用程序,所以下面的连接字符串显然有效。

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

一旦我尝试访问安全页面(基于角色的访问),我就会感到害怕:

System.Data.SqlClient.SqlException: 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)

这是另一个连接字符串:

<add name="CustomerIntranetConnectionString" connectionString="Data Source=SBS;Initial Catalog=CustomerIntranet;Integrated Security=True;"
  providerName="System.Data.SqlClient" />
  • 我通过远程桌面连接连接到服务器。
  • 我正在通过该服务器上的浏览器运行应用程序
  • SQL 服务器 (SQLSERVER 2005) 已配置为远程访问
  • 我可以登录了
  • ASP.NET Membership 和其他数据库内容都位于同一个数据库中
  • 应用程序部署在 IIS 6.0 上
  • 应用程序在我的机器上运行良好 (IIS 7.0)

如果有人能对这个问题有所了解,那就太棒了。我玩过连接字符串,但成功率为零。

重申一下,我可以登录,所以我显然建立了与数据库的连接。一旦我点击安全页面,我就会收到错误消息。

谢谢。

PS 这是堆栈

[SqlException (0x80131904): 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)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009598
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) +341
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +129
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +270
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +195
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +232
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +185
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +33
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +524
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
   System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +479
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +108
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
   System.Data.SqlClient.SqlConnection.Open() +125
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +123
   System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +166
   System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +115
   DataAccess.IntranetTableAdapters.employeeTableAdapter.GetEmployeeByUserID(Nullable`1 id) +377
   BusinessClasses.GlobalOperations.getEmployeeByUserID(Guid id) +96
   Customer.EmployeeTimesheet.prepareComponents() +327
   Customer.EmployeeTimesheet.Page_Load(Object sender, EventArgs e) +179
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

这是完整的配置文件:

<?xml version="1.0"?>

<configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="Data Source=SBS;Initial Catalog=CustomerIntranet;Integrated Security=True;"
      providerName="System.Data.SqlClient" />
    <add name="IntranetConnectionString" connectionString="Data Source=SBS;Initial Catalog=CustomerIntranet;Integrated Security=True;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="8192" executionTimeout="360"/>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

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

    <roleManager enabled="true" defaultProvider="MyRoleProvider">
      <providers>
        <clear/>
        <add name="MyRoleProvider" type="BusinessClasses.MyRoleProvider, BusinessClasses"  />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

【问题讨论】:

  • 如果您使用相同的初始目录 - 为什么不为所有内容使用相同的连接?试试这个看看 MARS 是否有帮助:
  • 感谢您的意见。我不是 100% 确定,但我认为 ASP.NET 成员需要 ApplicationServices 的东西,因此它有自己的标签。我尝试了您的建议,但没有任何改变。

标签: asp.net sql-server-2005 connection-string


【解决方案1】:

这个连接字符串实际上是把控制权交给系统

Data Source=SBS;Initial Catalog=CustomerIntranet;Integrated Security=True;

我说的控制,是指系统和数据库一起检查他是否允许连接到数据库。所以这就是适用于一种情况而不适用于另一种情况的原因。

我的建议是,首先在Data Source=localhost 上使用localhost,除非您连接到另一台计算机,然后确保在服务器的host 文件上正确配置了计算机的名称,或者直接使用知识产权。

再次打开数据库管理并确保目录存在,并且尝试连接的池具有读取该目录的权限。此外,数据库的文件必须具有池的权限。

池在用户帐户或系统帐户下运行。该帐户必须具有读取/写入该数据库的权限,无论是在数据库的文件上还是在数据库的首选项上。此外,该用户必须在数据库的总列表中。

【讨论】:

  • 对不起。应用程序池标识是网络服务。我的 SQL Server 有一个 NT AUTHORITY\NETWORK SERVICE 的登录名,而数据库有一个用户 NT AUTHORITY\NETWORK SERVICE。此用户被授予 dbo.owner 权限。我可以使用 Data Source=localhost 或 SBS 或 127.0.0.1 登录到 Web 应用程序。我不明白您的评论“系统与数据库一起检查他是否允许连接到数据库。所以这就是在一种情况下有效而在另一种情况下无效的原因。”这并没有真正向我解释为什么它适用于一个而不是另一个。你能详细说明一下吗?谢谢
  • @spryce 系统指的是您对数据库文件设置的权限。这些文件必须允许池的用户能够读取和写入。现在,在您修复该问题后,转到数据库并允许同一用户具有读取和写入权限。如果数据库有网络服务,那么请仔细检查该池是否在网络服务下运行,并且该文件具有网络服务的读/写权限。
  • NT AUTHORITY\NETWORK SERVICE 在指定数据库的 SQLServer 中具有 db_owner、db_datareader、db_datawriter 角色成员身份。我什至去了 CustomerIntranet.mdf 并授予 NT AUTHORITY\NETWORK SERVICE 的读、写权限(只是为了它)。正如我提到的“应用程序池标识是网络服务”。对我来说不幸的是,这意味着我已经完成了你所建议的一切,我仍在寻找关于为什么我只能访问我的数据库中的 ASP_ 表的答案。在 IIS7 + SQLServer2008 中运行良好。不过感谢您的帮助
【解决方案2】:

我发现了这个问题的问题所在。让我先添加一些与该问题相关的更多信息。

我正在使用具有典型 DataAccess、DataClasses(类型化数据集、xsd 文件)、业务和表示层的 n 层架构。

在解决方案范围内搜索 connectionString 会发现多个硬编码的连接字符串(由 VS 提供)未被 web.config 覆盖。这些显然是在设置 DataSet 设计器期间创建的。

在部署应用程序时,web.config 连接字符串反映的是生产服务器,但硬编码的字符串仍然引用我的开发服务器。

我的有限理解是 web.config 应该覆盖应用程序中其他地方定义的任何其他连接字符串,如果它们具有相同的名称。似乎情况并非如此。比我更聪明的人可能会详细说明这一点。

简而言之,如果您像我一样遇到任何奇怪的问题,请在解决方案范围内搜索任何其他连接字符串。它们可能在您不知情的情况下存在,并且可能导致冲突。

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多