![](/img/trans.png)
[英]Why does my application hang while trying to close a SqlConnection object?
[英]Why ThreadAbortException when trying to close a SqlConnection in .NET?
我这样做时总是收到以下异常:
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方法-直到那时-把它弄糟!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.