I have a class in C# called SQLServerConnection
that holds a SqlConnection
object. The class connects to a SQLServer with the SqlConnection
instance. The class has a destructor as follows:
~SQLServerConnection()
{
dbConnection.Close();
}
Upon program completion, an error is thrown at dbConnection.Close()
with the following error:
System.InvalidOperationException: 'Internal .Net Framework Data Provider error 1.'
Any idea why this is happening? If I call Close()
normally in the main class this works fine but only throws an error if done in the destructor.
Also, is it bad practice to close connections in destructors like this?