C# ASP.NET MVC Web App用户登录后,SQL Server Express连接失败,且没有任何有用的错误信息

C# ASP.NET MVC Web App用户登录后,SQL Server Express连接失败,且没有任何有用的错误信息,c#,asp.net,sql-server,entity-framework,visual-studio,C#,Asp.net,Sql Server,Entity Framework,Visual Studio,我使用的是Visual Studio 2013 Update 4,我有一个ASP.NET MVC Web应用程序,它使用的是Entity Framework 6.1.1 当我尝试运行我的web应用程序时,它加载失败,并出现错误 在控制台中,我反复出现相同的错误: A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll A first chance excep

我使用的是Visual Studio 2013 Update 4,我有一个ASP.NET MVC Web应用程序,它使用的是Entity Framework 6.1.1

当我尝试运行我的web应用程序时,它加载失败,并出现错误

在控制台中,我反复出现相同的错误:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
在web浏览器中,我遇到以下错误:

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: 26 - Error Locating Server/Instance Specified)

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. 

SQLExpress database file auto-creation error: 

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server account must have read and write access to the application's directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist.
If the application's App_Data directory already exists, the web server account only requires read and write access to the application's App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the application's App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server account's credentials are used when creating the new database.
Sql Server Express must be installed on the machine.
The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.


Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

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: 26 - Error Locating Server/Instance Specified)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1900630
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +545
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +452
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +198
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +923
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +324
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +646
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +459
   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +800
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +143
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +21
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +141
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +252
   System.Data.SqlClient.SqlConnection.Open() +130
   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +75

[HttpException (0x80004005): Unable to connect to SQL Server database.]
   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +125
   System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +89
   System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +29
   System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +386

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.5.27.0
因此,我尝试使用从Visual Studio中的属性窗口获取的连接字符串,使用SQL Server Management Studio连接到数据库:

Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\Users\Aaron\Desktop\AbcBankSolution\AbcBankWebApplication\App_Data\aspnet-AbcBankWebApplication-20150203085459.mdf;Initial Catalog=aspnet-AbcBankWebApplication-20150203085459;Integrated Security=True
这会产生以下错误:

The value's length for key 'data source' exceeds it's limit of '128'
这是奇怪的,因为它只有15个长度

最后,在Visual Studio中,我可以在服务器资源管理器窗口中连接并刷新数据库,所以我真的不知道发生了什么


如何解决此问题?

我觉得错误很明显,无法找到有问题的数据库,正在尝试在应用程序中自动创建数据库,但未能做到这一点

你试过这个零件吗

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102.

另外,当您将连接字符串复制并粘贴到Management Studio中时,它读取的是整个字符串,而不仅仅是连接字符串的数据源部分,这就是长度过长的原因。

我很清楚这个错误,它找不到有问题的db,正试图在您的应用程序中自动创建它,而没有做到这一点

你试过这个零件吗

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102.

此外,当您将连接字符串复制并粘贴到Management Studio中时,它读取的是整个字符串,而不仅仅是连接字符串的数据源部分,这就是长度过长的原因。

我尝试了许多不同的解决方案来修复此错误。最后,我发现出现问题的是异常被抛出的那一行,而不是调试器所说的那一行

我尝试了许多不同的解决方案来修复这个错误。最后,我发现出现问题的是异常被抛出的那一行,而不是调试器所说的那一行

数据源长度是
数据源=
之后的整行长度。就是

Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\Users\Aaron\Desktop\AbcBankSolution\AbcBankWebApplication\App_Data\aspnet-AbcBankWebApplication-20150203085459.mdf;Initial Catalog=aspnet-AbcBankWebApplication-20150203085459;Integrated Security=True
从行首开始(
数据源=
)的长度为247个字符。通过
Data Source=
将长度减少12个字符,将产生235个字符的长度。这远远超过了属性
数据源
允许的最大长度128个字符

将数据库放在短路径中,例如
C:\DSolution
。将它的名称缩减为类似于
ABS-201502038.mdf
的名称,记住所有的说明符,如
Initial Catalog=
;集成安全性=True
作为
数据源
属性长度的一部分计算。查看是否需要指定
初始目录


请记住,您必须制作从
(LocalDB)
的所有内容;Integrated Security=True
可容纳少于128个字符。因此,请缩短路径,并至少缩短名称。

数据源长度是
数据源=
之后的整行长度。就是

Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\Users\Aaron\Desktop\AbcBankSolution\AbcBankWebApplication\App_Data\aspnet-AbcBankWebApplication-20150203085459.mdf;Initial Catalog=aspnet-AbcBankWebApplication-20150203085459;Integrated Security=True
从行首开始(
数据源=
)的长度为247个字符。通过
Data Source=
将长度减少12个字符,将产生235个字符的长度。这远远超过了属性
数据源
允许的最大长度128个字符

将数据库放在短路径中,例如
C:\DSolution
。将它的名称缩减为类似于
ABS-201502038.mdf
的名称,记住所有的说明符,如
Initial Catalog=
;集成安全性=True
作为
数据源
属性长度的一部分计算。查看是否需要指定
初始目录


请记住,您必须制作从
(LocalDB)
的所有内容;Integrated Security=True
可容纳少于128个字符。因此,请缩短您的路径,并至少缩短您的姓名。

我无法评论Aalawlx的解决方案。。。但在我的情况下是一样的。
我有一行
Membership.GetUser().ProviderUserKey.ToString()
,调试器就在这行中停止,并给了我SQL错误。。。。该死,花了我很长时间和大量的Google:-)

我不能评论Aalawlx的解决方案。。。但在我的情况下是一样的。
我有一行
Membership.GetUser().ProviderUserKey.ToString()
,调试器就在这行中停止,并给了我SQL错误。。。。该死,花费了我很长时间和大量的Google:-)

数据库存在-我可以在Visual Studio中使用服务器资源管理器连接到它。。。SQLManagementStudio错误具体提到了“数据源”键。它指的是由其内部代码解释的数据源。我从未见过像您所描述的那样在一个输入框中包含整个连接字符串的SQL Management Studio版本。如果数据库存在,并且您可以在VS中连接到它,那么我建议您可能没有与VS使用的相同的连接信息。SQL Server Management Studio 2014的所有版本都将使用整个连接字符串-我不知道以前的版本。该选项位于选项>附加连接参数下。我必须重新安装SQL Server-感谢您的帮助!数据库存在-我可以使用服务器资源管理器在Visual Studio中连接到它。。。SQLManagementStudio错误具体提到了“数据源”键。它指的是由其内部代码解释的数据源。我从未见过像您所描述的那样在一个输入框中包含整个连接字符串的SQL Management Studio版本。如果数据库存在,并且您可以在VS中连接到它,那么我建议您可能没有与VS使用的相同的连接信息。SQL Server Management Studio 2014的所有版本都将使用整个连接字符串-我不知道以前的版本。T