简体   繁体   中英

Why ThreadAbortException when trying to close a SqlConnection in .NET?

I keep getting the following exception when I do:

Using cnn As SqlConnection = New SqlConnection(ConnectionStr)
    cnn.Open()  'I am fine up to here'
End Using       'Here I am getting the following exception'

Manually called cnn.Dispose() causes the same exception. It seems to be OK in most places in my code but just in this one function I can't close the connection that I opened because I keep getting the ThreadAbortException. I am stumped, any ideas? any hints? Here is the exception I get:

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)

There was a dreaded "Finalize" method that someone put. It kept triggering and trying to close the connection. That was a couple of hours wasted!!! One day am going to get rid of all the Finalize methods in the code - until then - suck it up!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM