c# - 为什么在 .NET 中尝试关闭 SqlConnection 时出现 ThreadAbortException?

标签 c# .net vb.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”方法。它不断触发并试图关闭连接。那是浪费了几个小时!总有一天我会摆脱代码中的所有 Finalize 方法 - 在那之前 - 接受吧!

关于c# - 为什么在 .NET 中尝试关闭 SqlConnection 时出现 ThreadAbortException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336190/

相关文章:

c# - EF如何附加实体

c# - 处置后台 worker

c# - 使用 C# 和 PHP 更新 mysql

c# - 使用MenuItem值作为CommandParameter

.net - 基础商务舱 : is it bad?

.net - 所有项目的 VB.Net 和命名空间

.net - 带有更改文章名称的 ASP.NET 路由

c# - 缺少系统.Windows

vb.net - 关闭 Windows 窗体应用程序和 excel.exe 进程如何交互(可能通过垃圾收集器)?

c# - ChrW(e.KeyCode) 的 C# 等价物是什么?