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...