本地IIS无法连接到LocalDB

时间:2017-02-22 03:22:13

标签: asp.net sql-server iis

我几乎不想问这个问题,因为这个问题似乎有很多变化,但到目前为止没有任何帮助。

我试图站起来几年前运行良好的旧ASP.Net 2.0应用程序(不同的计算机/环境。)我可以得到默认/主页面显示没有问题。但是,所有数据库调用都失败。我可以通过SSMS连接到“(localdb)\ MSSQLLocalDB”(使用Windows身份验证或预期的应用程序凭据。)选择在所有表/视图/ sprocs上通过SSMS运行的语句没有问题(所有这些都是空的,但它们存在。)

异常

Server Error in '/' Application.
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: 50 - Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start.)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: 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: 50 - Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start.)

Source Error:

Line 64:     SqlDataAdapter da =  new SqlDataAdapter(sql, connection);
Line 65:     DataSet ds = new DataSet();
Line 66:     da.Fill(ds);

Source File: c:\{snip}\Page.aspx.cs    Line: 66

Stack Trace:
[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: 50 - Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start.
)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4876207
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject) +354
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +90
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +367
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +225
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +189
   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) +31
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +431
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
   System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +499
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +65
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117
   System.Data.SqlClient.SqlConnection.Open() +122
   System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState) +31
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +112
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +287
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +94
   ExportProgress.Page_Load(Object sender, EventArgs e) in c:\Users\qamain\Desktop\IDC\code\web\InstructionalDays\ExportProgress.aspx.cs:66
   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) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.5420; ASP.NET Version:2.0.50727.5459 

连接字符串

<connectionStrings>
    <add name="ConnStr" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyApp;User ID=MyUser;Password=****" providerName="System.Data.SqlClient"/>
</connectionStrings>

环境 Win7 Pro VM上的管理员帐户,ASP.Net 2.0,SQL(各种,见下图) Installed SQL bits, 2012-2016

通过VS 2015社区进行调试

我确保.Open语句确实获得了预期的连接字符串。

已验证的数据库信息

C:\Users\myuser>sqllocaldb info mssqllocaldb
Name:               mssqllocaldb
Version:            12.0.2000.8
Shared name:
Owner:              MYVM\myuser
Auto-create:        Yes
State:              Running
Last start time:    2/22/2017 1:33:53 AM
Instance pipe name: np:\\.\pipe\LOCALDB#E36AA52B\tsql\query

通过SQL Server 2016 Configuration Manager进行SQL设置

TCP/IP protocol enabled

IIS身份验证设置

这些指定与SQL DB的所有者相同的“myuser” IIS account auth passes and same as SQL owner

事件记录

这些是单次尝试命中页面/数据库的事件。警告与此帖子顶部列出的消息/例外相同。 SQL错误(不太有帮助)重复五次(如下所示)。 enter image description here

疑难解答步骤

  1. 删除并重新创建了数据库
  2. 删除了以下路径项, C:\Users\myuser\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances
  3. 禁用防火墙以确保它不是问题
  4. 回收/重新启动的应用程序池和网站(多次)
  5. 在此文件中将loadUserProfilesetProfileEnvironment设置为true,C:\Windows\System32\inetsrv\config\applicationHost.config显示为here
  6. 我到底错过了什么?

1 个答案:

答案 0 :(得分:0)

如果值得保留,我会留给社区。

问题现在已经解决,遗憾的是,我不确切知道哪个步骤解决了这个问题。

我最近注意到,通过sqllocaldb.exe删除并重新创建MSSQLLocalDB条目,无法访问应用程序所需的数据库(以及其表/ views /等)。这很奇怪,因为当我尝试重新创建它时,SQL抱怨db.mdf文件已经在C:\Users\myser文件夹中。我在那里删除了MDF和LDF文件,然后只是重新创建了db(包含用户,表,视图,sproc),所有这些文件终于有效了。

呃,以后几天的工作。如果社区认为这个帖子是无关紧要的话,那么就没有违法行为(但也许对其他人来说,故障排除步骤可能是值得的。)