The reason that the samples in the microsoft documentation do not show calls
to Dispose for the SqlCommand object is that, as it has been stated in this
thread, this does nothing. Looking at the ildasm of SqlCommand shows that it
does not override dispose and just inherits it from the base. The person
writing the sample knew this and coded accordingly. I have written some of
the examples in the docs myself and have done the same thing.
The problem is that as the next version of the urt rolls along there are no
guarantees that dispose will keep doing nothing, it is also tedious to
figure out exactly what objects you have to dispose at all costs
(SqlConnection!) and which you can ignore completelly. The general rule of
"if it implements idisposable, dispose it" will always work.
I am familiar with the Quote you mention. This information was based on
outdated information, for Beta 1 the SqlConnection did not implement a
completelly managed pooling solution, instead it relied on Enterprise
services object pooling. There where a number of problems with this
implementation but it was always meant to be temporary, the documentation
was added to the Beta documentation to work arround some issues with Dispose
and it was only fixed by v1.1! (auch, my fault)
Calling Dispose on the SqlConnection does not close the connection to sql
server, the only thing that Dispose does internally is to set the connection
string to null and to call Close if the connection has not already been
closed. The recommended usage (for the same reasons as I am recomending
always calling dispose) is to call using Sqlconnection (which automatically
disposes) and to call connection.Close(), there is no perf penalty for
calling both.
Hope this helps
--
Angel Saenz-Badillos [MS] Managed Providers
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
Henrik Dahl said:
Angel,
Yes, that's obvious, but how may it then be that there are many, many
examples from Microsoft which do not invoke the Dispose() method of
SqlCommand objects, even examples which include Disposing e.g. the
SqlConnection object?
A question:
In one of the books from Microsoft Press it's stressed that you should
Close(), not Dispose(), an SqlConnection object because Dispose() will close
the connection to MS SQL Server whereas Close() lets the connection to go
back to the pool, i.e. closes the logical connection. In the ".NET Data
Access Architecture Guide" it's claimed that both Close() and Dispose() let
the SqlConnection to go back to the pool. Which is correct?
Best regards,
Henrik Dahl
this
is
very easy to do with C# and the "using" keyword, in other .net
languages
you
can use the try finally construct to ensure that Dispose gets called. In
general adhering to this basic principle will greatly reduce stress related
problems with your code and may increase performance as it makes
cleaning
up
smarter.
Something else to think about is that allthough currently the Command may
not be doing anything meaningfull in its dispose method, there is no
guarantee that this will be true for future releases of the framework.
Calling dispose for all disposable objects will greatly enhance the lifetime
of your code.
Hope this helped,
--
Angel Saenz-Badillos [MS] Managed Providers
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
Hello!
After I've finished using an instance of the SqlCommand class,
should
free