Embedded with a good idea

This is the first time I'll use nested using. Am I doing it right? Following are the codes:

public Boolean AddToDigitalAssets(string fileName)
{
    ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
    Boolean result = true;
    using (SqlConnection cnn = new SqlConnection(_ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
             using (SqlDataAdapter da = new SqlDataAdapter(cmd))
             {

             }
        }
    }
    return result;
}
+3
source share
2 answers

Yes, that's great Validand good practice.

you can even remove the curly braces for the first use of the block, as shown below:

using (SqlConnection cnn = new SqlConnection(_ConnectionString))            
using (SqlCommand cmd = new SqlCommand())
{

}
+4
source

the block used "guarantees the deletion of the resource ... even in the case of an unhandled exception."

"" . . , ? , finally, using? Environment.FailFast? , . , . , , .

, , : - , #. finally finally , , - , , . , , , ; # promises , , . . . , ; ?

- ?

.

, , , ?

. , using. .

, , , . -, , , . , .

+2

All Articles