Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SqlDataAdapter.Dispose actually Close an associated SqlConnection?

Tags:

.net

ado.net

Does anyone know if the SqlDataAdapter.Dispose method actually closes or disposes any SqlConnections? I loaded up Reflector and I see that SqlDataAdapter inherits from DbDataAdapter. If I disassemble and look at the dispose method in that class, there appears to be no disposal of any SqlConnections. I suppose I could write a test for this, but I figured I would ask to see if anyone had any insight on this.

like image 637
Page Avatar asked Jan 22 '09 15:01

Page


People also ask

Does disposing SqlConnection close connection?

If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose . Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes , the underlying connection is returned back to the connection pool.

Do you need to dispose Sqldataadapter?

Closing SqlDataReader Always remember to close and dispose your SqlDataReader , just like you need to close the SqlConnection . In fact, SqlCommand also requires disposing (there is no Close method).

Is SqlConnection disposable?

Yes. But only if the class is the owner of said resource. You don't want to dispose of a connection that someone else is using. And this has nothing to do with the Garbage Collector, by the way.

Do I need to Dispose DataTable?

The Dispose() methods in DataSet and DataTable exists ONLY because of side effect of inheritance - in other words, it doesn't actually do anything useful in the finalization. Dispose is for handling the disposal/release of unmanaged resources or other managed resources that have releasable items, such as SqlConnection.


2 Answers

The first thing to be aware of is the DataAdapter does manage and close your connection in some circumstances. For example, if you're using a DataAdapter you're probably operating on DataTable/DataSet using .Fill() and .Update() functions.

From the .Fill() docs:

The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.

The .Update() docs don't mention anything about the connection at all, so I would expect to need to manage it manually.

You asked specifically about the Dispose() method. Like Update(), the Dispose() docs don't specifically mention the connection, so I would expect to need to close it manually.

Finally, we can improve on Bob King's code slightly like this:

Using conn as New SqlConnection(""), _
      adapter as New SqlDataAdapter() With {.Connection = conn}
    ' Do stuff
End Using

Or in C#:

using (SqlConnection conn = new SqlConnection(""))
using (SqlDataAdapter adapter = new SqlDataAdapter() {Connection = conn})
{
    // Do stuff
}
like image 111
Joel Coehoorn Avatar answered Dec 30 '22 20:12

Joel Coehoorn


As far as I know it does not. I use nested Using statements to achieve this, create the connection first, then create the adapter and as the using statements "pop", the adapter is Disposed, then the connection is Disposed, which does trigger a close:

Using conn as New SqlConnection("")
    Using adapter as New SqlDataAdapter() With {.Connection = conn}
        'Do stuff'
    End Using
End Using

The syntax is virtually identical for C#, if that's your language of choice.

like image 31
Bob King Avatar answered Dec 30 '22 19:12

Bob King