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.
Nope, it doesn’t dispose the connection. I believe it shouldn’t. You might want to use it elsewhere.
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:
The syntax is virtually identical for C#, if that’s your language of choice.
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
.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. LikeUpdate()
, theDispose()
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:
Or in C#: