I have a backgroundworker in my View which runs a sql query in my DAL.
On my view I also have a progress bar which keeps spinning so the user knows work is being done.
The SQL query receives as one of the conditions a List<string>
, that I populate in one of the where conditions, so the code is something like this:
using (SqlConnection conn = new SqlConnection(_myHost))
{
conn.Open();
foreach (string item in myList)
{
query = "select * from ( " +
"//big query with inner joins here" +
") a "
using (SqlCommand cmd = new SqlCommand(query, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
}
}
So as you notice above, I run through all the items in my list querying them individually on the where clause (omitted above) and add the output, if any, to a datatable, which once the foreach is completed, will be returned to my view layer and become the datasource of a gridview.
It occurred to me that while I cannot query the server to tell me which % of the query is completed, maybe I could be able to provide the % completed of the foreach loop, which is some information to the user.
What I am trying to figure out is a way to:
Return to my view, the current iteration of the loop, which I could detected by simple adding a int i
counter and increment it on each foreach, or maybe a string item
in the beginning to inform what is being queried.
Thing is, I do not know how to return that value to my view as the codes execute. I considered C#4 Tuple, but it will not work because that is returned only once the method is completed and not during the foreach
execution.
Any ideas?