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.
Does anyone know if the SqlDataAdapter.Dispose method actually closes or disposes any SqlConnections? I
Share
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#: