Using a statement - is it more useful to connect sql than to sql adapter?

I know that the following statement is useful for garbage collection. In fact, it should be the same as Try / Catch / finally. If I use it to connect sql, should I also add another 'using' statement for the sql adapter? And then another nested use for a DataSet?

using() { } 

Another addition to the question: if I want to use the same connection in several methods, is it better to use "Using ()" in each of the methods, or can I just create this connection object once?

+4
source share
4 answers

Yes, you should use the using statement to place everything that implements IDisposable . The using statement does not apply to garbage collection, but to resources (other than memory). Both DataSet and SqlDataAdapter are instances of IDisposable .

The using block means that you guarantee that the resources that are stored in the object are allocated in a timely, deterministic manner. Without using there is no guarantee that these resources will be freed. This can lead to a leak of resources, for example, to the termination of work with files.

+4
source

Something else worth noting is that you can combine several using statements into one block as follows:

 using (SqlConnection connection = new SqlConnection("connectionString")) using (SqlCommand command = new SqlCommand("SELECT * FROM Users", connection)) { // Do something here... } 

As soon as you exit this block (either reaching the end of the block or throwing this exception), the SqlConnection and SqlCommand objects will be deleted automatically.

+9
source

using not the same as try/catch/finally !! using ensures that Dispose automatically called in the IDisposable in parentheses, so for now

 using (SqlConnection conn = new SqlConnection(...)) { } 

coincides with

 SqlConnection conn = new SqlConnection(...); try { } finally { conn.Dispose(); } 

there are no capture locks involved at all, no errors are caught!

If you want to get rid of all disposable objects that you create in a timely manner, then yes, you should use the using block for each of them.

Sometimes it’s easier to read everything outside the try block and then delete everything in one finally block (an example for this would be painting in Windows Forms applications. Sometimes you need a lot of brushes and pens. You can create them before try , use them in the block and destroy them in the finally block).

This is basically a coding style issue.

+5
source

The using status is intended to be used for each instance of the class that implements the IDisposable interface. His connection with the GC (garbage collector) is that your instances release their resources before the GC does. This is very useful for shared resources and helps with concurrency.

As for your question, yes, you should use the using statement in Connection and Adapter.


As for the try and catch block, there is no resemblance to the using statement. Both of them are used for different purposes. One (used) is used to release resources early, the other is used to handle errors regardless of whether they are unforeseen or expected.

+4
source

Source: https://habr.com/ru/post/1401322/


All Articles