0

I have a noob question, it is always necessary to have using(MySqlDataAdapter ...) and using(MySqlCommand)? Or This is a bad idea?

for example, I have this code:

 using (MySqlConnection connection = new MySqlConnection(GlobalVariables.connectionSQL))
 {
    connection.Open();
    using (MySqlCommand cmd = new MySqlCommand(consulta, connection))
    {
        using (MySqlDataReader reader = cmd.ExecuteReader())
        {
            int cantImgs = 0;
            string nument = "";

            while (reader.Read())
            {
                ObjetctList list = new ObjectList();
                list.listNumber = reader["Number"].ToString();
            }
            foreach (var list in listViewModels)
            {
                if (nument != list.listNumber )
                {
                    cantImgs++;
                }
                nument = list.listNumber ;
            }

            if (cantImgs == 0)
            {
                ViewData["EntregasInexistentes"] = "No Data.";
            }
        }
        return listViewModels;
    }
}
try
{
    using (MySqlConnection connection = new MySqlConnection(GlobalVariables.connectionSQL))
    {
        connection.Open();
        using (MySqlCommand cmd = new MySqlCommand(consulta, connection))
        {
            cmd.CommandType = System.Data.CommandType.Text;
            using (MySqlDataReader mySqlDataReader = cmd.ExecuteReader())

            return true;
        }
    }
}

Is this Ok? Or is not necessary to have using inside a using? I know that the first statement (using (MySqlConnection connection = new MySqlConnection(GlobalVariables.connectionSQL)) is ok because I need to autommatically close the connection when is all over...

D Stanley
  • 149,601
  • 11
  • 178
  • 240
popo
  • 71
  • 6
  • 1
    It is a very common practice to have nested `using` statements. However you don't necessarily *need* a `using` statement here. All it does in this context is call `.Dispose()` on the variable defined within the statement after the code block is finished executing. You should always dispose these objects, but you don't necessarily need to use `using` statements to do it. – Jesse Dec 15 '22 at 19:00
  • Okey, so it is a good practise to use the `using` statement? – popo Dec 15 '22 at 19:03
  • 1
    Yes. `using` statements are a way for you to not need to worry about making sure you dispose the object yourself. It also makes it very easy to tell when the object is getting disposed and prevents the object from being used after it has been disposed since it's not accessible outside of the scope of the using statement. – Jesse Dec 15 '22 at 19:06
  • 2
    Beginning with C#8.0 you don't need the curly brackets what avoids the intending. It is then just `using MySqlConnection connection = new MySqlConnection(GlobalVariables.connectionSQL);` – Sebastian S. Dec 15 '22 at 19:08
  • And when does it close the connection when I don't use curly brackets? When I close the function/method where I'm using it? – popo Dec 15 '22 at 19:09
  • And last question, can this reduce CPU usage? Or has nothing to do – popo Dec 15 '22 at 19:12
  • 1
    The variable gets disposed at the end of its scope, e.g. at the end of the method. I cannot answer your last question, but I don't think that you can measure a reduction of the cpu usage. Maybe in edge-cases where you have a lot of usings in the scope. – Sebastian S. Dec 15 '22 at 19:19

0 Answers0