Disposing SQL objects inside another using block In one sql connection I have multiple sql commands and I wonder what approach is better - create and dispose SqlCommand for each operation or use only one sql command?

Eg:

using (SqlConnection connection = new SqlConnection(ConnectionString))
{
   connection.Open();

   using(SqlCommand cmd = new SqlCommand("select...", connection))
   {
       cmd.ExecuteNonQuery();

       cmd.CommandText = @"another query...";
       cmd.ExecuteNonQuery();

       //and so on
   }
}


or

using (SqlConnection connection = new SqlConnection(ConnectionString))
{
   connection.Open();

   using(SqlCommand cmd = new SqlCommand("select...", connection))
   {
       cmd.ExecuteNonQuery();
   }

   using(SqlCommand cmd = new SqlCommand("another query...", connection))
   {
       cmd.ExecuteNonQuery();
   }
}


If SqlCommand(s) are inside another using block (in this cae SqlConnection), disposing will be done after close connection or immediately after running SqlCommand?

I'm asking because I have web api which receives a few hundred requests per minute and my goal is to optimize application for better garbage collector working - there might be memory leak
Yes, you can do this, and the garbage collector will do the job well. This is very close to your original code (snippet 1), but notice the parameter clear instructions.

using (var sqlConnection = new SqlConnection(ConnectionString))
{
   sqlConnection.Open();

   using (var command = new SqlCommand("select...", sqlConnection))
   {
       command.ExecuteNonQuery();
       command.Parameters.Clear();  // &lt;-- IMPORTANT BEFORE RUNNING NEXT COMMAND

       command.CommandText = @"another query...";
       command.ExecuteNonQuery();
       command.Parameters.Clear();  // &lt;-- IMPORTANT BEFORE RUNNING NEXT COMMAND

       // ...etc
   }
}