title: Multithreading with ADO.NET
body: <p>I want to select some data from tables at the same time. When I am trying to do this with parallel threads an exception occurs. Should I use different <code>SqlConnection</code> objects for every <code>SqlCommand</code> or is there any technique to share same <code>SqlConnection</code> between several threads and execute several <code>SqlCommand</code> at the same time without exception.</p>
  <pre><code>SqlConnection connection = new SqlConnection(&quot;...&quot;)
  connection.Open();
  
  List&lt;Task&gt; tasks = new List&lt;Task&gt;();
  
  for (int i = 0; i &lt; 10; i++)
  {
      tasks.Add(Task.Run(() =&gt;
      {
          var command = connection.CreateCommand();
          command.CommandText = &quot;select top 1 * from Persons&quot;;
          var data = command.ExecuteReader();
          while (data.Read())
          {
              Console.WriteLine($&quot;{data[0]} {data[1]} {data[2]}&quot;);
          }
      }));
  }
  
  await Task.WhenAll(tasks);
  
  connection.Close();
  </code></pre>