为什么这些连接字符串中只有一个在实际相同时才起作用?

时间:2013-03-02 09:54:52

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

我有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成员资格和其他数据库内容都位于同一个数据库中
  • 该应用程序部署在IIS 6.0
  • 应用程序在我的计算机上运行正常(IIS 7.0)

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

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

感谢。

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>

2 个答案:

答案 0 :(得分:2)

此连接字符串实际上是将控件传递给系统

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

通过说控制,我的意思是系统与数据库一起检查他是否允许连接到数据库。所以这就是一个案例而不是另一个案例的原因。

我的建议是,首先在Data Source=localhost上使用localhost,除非您连接到其他计算机,然后确保在服务器的host文件上正确配置了计算机的名称,或者只需直接使用IP。

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

池正在用户帐户或系统帐户下运行。该帐户必须具有读取/写入该数据库的权限,包括数据库文件和数据库首选项。此用户也必须在数据库的宏列表中。

答案 1 :(得分:0)

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

我使用的是具有典型DataAccess,DataClasses(Typed数据集,xsd文件),Business和Presentation层的n层架构。

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

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

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

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