当我这样做时,我不断收到以下异常:
Using cnn As SqlConnection = New SqlConnection(ConnectionStr)
cnn.Open() 'I am fine up to here'
End Using 'Here I am getting the following exception'
手动调用 cnn.Dispose() 会导致相同的异常。在我的代码中的大多数地方似乎都可以,但只是在这个函数中我无法关闭我打开的连接,因为我不断收到 ThreadAbortException。我很难过,有什么想法吗?有什么提示吗?这是我得到的异常:
System.TypeInitializationException: The type initializer for 'System.Data.ProviderBase.DbConnectionClosedPreviouslyOpened' threw an exception. ---> System.Threading.ThreadAbortException: Exception of type 'System.Threading.ThreadAbortException' was thrown.
--- End of inner exception stack trace ---
at System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlInternalConnection.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Close()
at System.Data.SqlClient.SqlConnection.Dispose(Boolean disposing)
最佳答案
有人提出了一个可怕的“Finalize”方法。它不断触发并试图关闭连接。那是浪费了几个小时!总有一天我会摆脱代码中的所有 Finalize 方法 - 在那之前 - 接受吧!
关于c# - 为什么在 .NET 中尝试关闭 SqlConnection 时出现 ThreadAbortException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336190/