Answer the question
In order to leave comments, you need to log in
C#. Does the DataAdapter need to call Dispose?
Do I need to call the Dispose method or throw it in using?
adapter = new SqlDataAdapter(cmd);
// ... actions
adapter.Dispose(); // здесь нужно?
Answer the question
In order to leave comments, you need to log in
Use the using construct - you can never go wrong - if you need to call Dispose - the compiler will call it for you.
There is always a sense in calling the Dispose method when there is the Dispose method itself (and to be more precise, if the System.IDisposable interface is implemented ). It's simply not your responsibility to figure out whether you need to call Dispose or not. If the class author has implemented this interface, then he needs it for something. As a rule, this is necessary if manually managed resources are used (for example, open files), or other objects are created in the object that are also IDisposable, and they must be cleaned up when disposing the main object that created them.
If you have come across C++, then in my opinion, Dispose is the real "destructor" for an object, which you need to call yourself (directly or indirectly, using using) in order to gracefully end the object's life cycle. Either way, call Dispose whenever you're done interacting with an object. It is important that even if nothing much happens NOW in the implementation of this method, the functionality can be added LATER in the new version of the library you are using. And then you have to fix your code to avoid leaking resources (unclosed files or database connections are often a much bigger problem than leaking memory).
For Nipheris
There are problems with spaces...
This is how the code will look like:
using (SqlConnection con = ...)
{
using (SqlCommand cmd = ...)
{
using (SqlReader reader = ...)
{
// ...
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question