title: Multithread SQL select statements
body: <p>I am a multithreading novice and a SQL novice, so please excuse any rookie mistakes.</p>
  
  <p>I am trying to execute many SQL queries asynchronously. The queries are all select statements from the same table in the same database. I can run them synchronously and everything works fine, but testing a small subset leads me to believe that to run all the queries synchronously would take approximately 150 hours, which is far too long. As such, I'm trying to figure out how to run them in parallel.</p>
  
  <p>I have tried to model the code after the answer at <a href="https://stackoverflow.com/questions/8815147/run-a-method-multiple-times-simultaneously-in-c-sharp">run a method multiple times simultaneously in c#</a>, but my code is not executing correctly (it's erroring, though I do not know specifically how. The code just says an error occurs).</p>
  
  <p>Here is what I have (A much smaller and simpler version of what I am actually doing):</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          List&lt;string&gt; EmployeeIDs = File.ReadAllLines(/* Filepath */);
          List&lt;Tuple&lt;string, string&gt;&gt; NamesByID = new List&lt;Tuple&lt;string, string&gt;&gt;();
  
          //What I do not want to do (because it takes too long) ...
          using (SqlConnection conn = new SqlConnection(/* connection string */))
          {
              foreach (string id in EmployeeIDs)
              {
                  using (SqlCommand cmd = new SqlCommand("SELECT FirstName FROM Employees WITH (NOLOCK) WHERE EmployeeID = " + id, conn))
                  {
                      try
                      {
                          conn.Open();
                          NamesByID.Add(new Tuple&lt;string, string&gt; (id, cmd.ExecuteScalar().ToString()));
                      }
                      finally
                      {
                          conn.Close();
                      }
                  }
              }
          }
  
  
          //What I do want to do (but it errors) ...
          var tasks = EmployeeIDs.Select(id =&gt; Task&lt;Tuple&lt;string, string&gt;&gt;.Factory.StartNew(() =&gt; RunQuery(id))).ToArray();
          Task.WaitAll(tasks);
          NamesByID = tasks.Select(task =&gt; task.Result).ToList();
      }
  
      private static Tuple&lt;string, string&gt; RunQuery(string id)
      {
          using (SqlConnection conn = new SqlConnection(/* connection string */))
          {
              using (SqlCommand cmd = new SqlCommand("SELECT FirstName FROM Employees WITH (NOLOCK) WHERE EmployeeID = " + id, conn))
              {
                  try
                  {
                      conn.Open();
                      return new Tuple&lt;string, string&gt; (id, cmd.ExecuteScalar().ToString());
                  }
                  finally
                  {
                      conn.Close();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Note: I do not care exactly how this is multithreaded (tasks, parallel.foreach, backgroundworker, etc). This is going to be used to run ~30,000 select queries exactly 1 time, so I just need it to run fast (I'm hoping for ~8 hrs = one work day, but I'll take what I can get) one time. It doesn't have to really be pretty.</p>
  
  <p>Thank you in advance!</p>