【发布时间】:2017-08-05 01:42:02
【问题描述】:
所以,我正在尝试使用 C# 制作一个 ASP.NET Web 窗体应用程序(请注意,我是 ASP 和 C# 的绝对初学者)。我正在关注位于此处的教程:
我在这台 Windows 7(64 位)机器上使用 Visual Studio 2013 和 SQL Server 2012 Express LocalDB。但是,当我在学校时,我在 Windows 10(64 位)机器上使用 Visual Studio 2015(具有相同的 SQL Server 版本)。
现在,我在上面的 VS2013 上编写本教程,在构建和运行程序之后,它可以工作了。将其移植到 VS2015 并运行程序后,我无法在 VS2013 上再次运行它。相反,我遇到了一个错误。
这是完整的错误文本:
An exception of type 'System.Data.SqlClient.SqlException' occurred in
mscorlib.dll but was not handled in user code
Additional information: 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. The
specified LocalDB instance does not exist.)
这里是源错误代码块(具体出现在第 24 行):
Line 22: var roleMgr = new RoleManager<IdentityRole>(roleStore);
Line 23:
Line 24: if (!roleMgr.RoleExists("canEdit"))
Line 25: IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
Line 26:
这是错误页面上显示的堆栈跟踪:
[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. The specified LocalDB instance does not exist.
)]
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) +821
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +332
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +699
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +89
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +426
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +191
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +154
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +21
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +90
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +217
System.Data.SqlClient.SqlConnection.Open() +96
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c) +36
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +138
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) +477
System.Data.Entity.SqlServer.<>c__DisplayClass33.<UsingConnection>b__32() +520
System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +34
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +251
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) +196
System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act) +276
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act) +545
System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +151
System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +287
System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +111
System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() +236
System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) +143
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +207
System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +42
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +136
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) +175
System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) +150
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +444
System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +290
System.Data.Entity.Internal.<>c__DisplayClassf`1.<CreateInitializationAction>b__e() +165
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +110
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +660
System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +31
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +143
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +292
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +123
System.Data.Entity.Internal.InternalContext.Initialize() +42
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +137
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +38
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +99
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken) +197
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate) +233
Microsoft.AspNet.Identity.EntityFramework.RoleStore`3.FindByNameAsync(String roleName) +1120
Microsoft.AspNet.Identity.<FindByNameAsync>d__13.MoveNext() +278
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +123
Microsoft.AspNet.Identity.<RoleExistsAsync>d__d.MoveNext() +612
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +244
Microsoft.AspNet.Identity.RoleManagerExtensions.RoleExists(RoleManager`2 manager, String roleName) +241
Starlane.Logic.RoleActions.AddUserAndRole() in F:\WEBENGINEERING\Starlane\Starlane\Logic\RoleActions.cs:24
Starlane.Global.Application_Start(Object sender, EventArgs e) in F:\WEBENGINEERING\Starlane\Starlane\Global.asax.cs:28
[HttpException (0x80004005): 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. The specified LocalDB instance does not exist.
)]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +10063289
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296
[HttpException (0x80004005): 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. The specified LocalDB instance does not exist.
)]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10044576
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
每当我注释掉任何使用 roleMgr var 的代码行时,应用程序就会运行,但是当我尝试测试登录系统时遇到相同的 SqlException 错误。我在想这可以通过我的 Web.Config 文件来纠正。这是我看到的(经过一些 Google 研究)我认为可能相关的部分代码:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Starlane-20170802095040.mdf;Initial Catalog=aspnet-Starlane-20170802095040;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="Starlane" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\starlane.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
使用服务器资源管理器,我可以查询 Starlane 数据库,但不能查询默认连接数据库。
无论如何,我希望我提供了足够的信息,以便有人能够帮助我解决这个问题。从 2015 年到 2013 年运行时,代码或任何配置是否有任何变化会导致此问题?
非常感谢您。
【问题讨论】:
-
您对 vs 版本感到困惑,这与 SQL 异常无关。 使用服务器资源管理器,我可以查询 Starlane 数据库,但不能查询默认连接数据库。 - 如果您无法查看数据库,您希望连接字符串如何工作? Google:错误:50 - 发生本地数据库运行时错误。指定的 LocalDB 实例不存在。
-
感谢您的帮助。我对版本切换的想法太多了,它肯定把我引向了错误的方向。我应该在发布之前进行更多研究,因为另一篇帖子确实有帮助:stackoverflow.com/questions/26007913/…
标签: c# asp.net sql-server visual-studio-2013 visual-studio-2015