0

因此,我試圖使用C#製作ASP.NET Web窗體應用程序(僅需注意,我是一個絕對的初學者與ASP和C#)。我在下面設在這裏的教程:在mscorlib.dll中發生類型'System.Data.SqlClient.SqlException'的異常,但未在用戶代碼中處理

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/membership-and-administration

我使用Visual Studio 2013的SQL Server 2012中快速的LocalDB此Windows 7(64位)的計算機上。但是,當我在學校時,我在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的代碼行,那麼a應用程序將運行,但當我嘗試測試登錄系統時遇到相同的SqlException錯誤。我在想這可以通過我的Web.Config文件來糾正。下面是代碼,我看到了,我想象會是相關的部分(後一些谷歌的研究):

<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年運行時,是否會有代碼或任何配置發生變化,導致此問題?

非常感謝你提前。

+0

你對vs版本感到困惑,它與SQL異常無關。 *使用服務器資源管理器,我可以查詢Starlane數據庫,但不能查詢默認連接數據庫。* - 如果無法查看數據庫,您希望連接字符串如何工作? Google:**錯誤:50 - 發生本地數據庫運行時錯誤。指定的LocalDB實例不存在。** –

+0

感謝您的幫助。我正在考慮版本切換太多,這肯定導致我走錯了方向。我應該已經研究了一些更多的發佈之前,另一篇文章做了幫助:https://stackoverflow.com/questions/26007913/the-specified-localdb-instance-does-not-exist – pandorachaos

回答

0

您需要檢查實例是否存在。 嘗試通過cmd提示符運行sqllocaldb.exe。

使用「sqllocaldb i」列出所有LocalDB實例。 使用「sqllocaldb創建實例名稱」來創建新實例。 使用「sqllocaldb start instance name」來啓動它。

+0

所以我完成的是看看實例和v11.0被停止了,所以我開始了它。我也創建了MSSQLLocalDB實例並啓動它。然後,我的項目給出了錯誤,指出數據庫已經創建,所以我將它們移出App_Data文件夾(我可以刪除它們,因爲它們沒有太多變化),並且項目在文件夾中創建了新的數據庫,並且有效!非常感謝幫忙。 – pandorachaos

相關問題