In most data providers, Connection.Dispose simply calls Connection.Close, and, in case of SqlClient, NULLs out the connection string so you can't use the Connection again without replacing it. In most providers, Command.Dispose is not strictly required, but, in at least one case, (OleDb data provider + MSDAORA OLE DB provider), Command.Dispose releases any internal database cursors, so if you don't call it, you will indeed run out of cursors. There is at least one ADO.NET book out that discusses this. ;-)
Database programmers are used to calling Close; Dispose is .NET convention, so, for the Connection both are provided. Command.Dispose is not used by all provider writers, but can be used to clean up, because, for instance, there is a Command.Prepare method but no Command.Unprepare. IDbConnection and IDbCommand both inherit from IDisposable, therefore a Dispose method must be coded, even if it is only a no-op (or delegates to Close). Please post to the new lists in future. Hope this helps, Bob Beauchemin [EMAIL PROTECTED] -----Original Message----- From: Chris Anderson [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 11, 2002 7:40 AM To: [EMAIL PROTECTED] Subject: Re: [DOTNET] ADO.NET and Dispose > > Basically..Am I going overkill on the Dispose methods? > > Not at all. If a class implements IDisposable, that means it > wants to call Dispose the absolute moment you're through. In > the case here, it means database connection resources are > returned to the pool to be used by others. I believe (thought > I have not tested) that if you weren't calling Dispose there, > you would soon run out of connection resources and > connections to the database would fail. I can understand that on the connection object It seems weird on the command object, etc (especially as I'm linking them all together to fire off a single statement to a single database.surely only one would be responsible for connection resources?) I'll probably end up looking through the IL of the various Dispose methods and see if there's any logic that calls the Dispose methods of the other related objects If I find anything I'll obviously post it ..well..probably not here, but on one of the new lists :-) ) > the ADO.NET > > books I've looked through rarely call Dispose at all > (perhaps with the > > same reasoning that error handling is rarely shown in > books..to make > > the point of the example clear) > > This would be a significantly damning statement about an > ADO.net book that didn't call Dispose (or at least Close). > Something like that should always, always be done. Yeah I was quite surprised that it wasn't mentioned (or it was rarely mentioned) I have so far just skimmed the books though, so perhaps I've missed it :-) Thanks Merak You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.