I am trying to figure out how to perform a 'For Each' loop for each distinct value returned from an SQL query.
Here is my pseudo 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)
In essence I need to be able to loop until I have a datatable per customer exported.
Hope that makes sense, any help greatly appreciated.
Select * FROM [Billing] ORDER BY CustomerSELECT * FROM Billing ORDER BY Customer). Then you can filter on each customer and get rows related to that customer. That would be much more efficient than looping and filling data for each customer separately.