title: 'For each' loop based in SQL select statement
body: <p>I am trying to figure out how to perform a 'For Each' loop for each distinct value returned from an SQL query.</p>
  
  <p>Here is my pseudo code.</p>
  
  <pre><code>connection.ConnectionString = "server=***01\SQLEXPRESS; database=Billing; integrated security=yes"
  command.Connection = connection
  command.CommandType = CommandType.Text
  command.CommandText = "SELECT DISTINCT [Customer] FROM [Billing]
  
  For Each... Distinct value returned above
  
      command.CommandType = CommandType2.Text
      command.CommandText2 = "Select * FROM [Billing] WHERE [Customer] = [DISTINCT VALUE FROM ABOVE]
      dataAdapter.SelectCommand = command
  
          'Fill data to datatable
          connection.Open()
          dataAdapter.Fill(datatableMain)
          connection.Close()
  
  Then Export (I am ok with the Export code)
  </code></pre>
  
  <p>In essence I need to be able to loop until I have a datatable per customer exported.</p>
  
  <p>Hope that makes sense, any help greatly appreciated.</p>