I am Developing C# WPF Simple application. This application consists of several frames to load depending on the database.
For each table, I have one Frame and all are linked to the main page just for navigation. I created Loading frame which consists of a simple progress bar.
That was the introduction now here is my question.
I want to link the ProgressBar with collecting results from the database.
For example this order: Select * from Tablename
I want the ProgressBar to launch, while the results are collected.
I am using Query on my database here is my method:
this on my DataAccessLayer:
public static DataTable ExecuteTable(string query, CommandType type, params SqlParameter[] arr)
{
SqlConnection cn = new SqlConnection(con);
cn.Open(); // connection
SqlCommand cmd = new SqlCommand(query, cn);
cmd.CommandType = type;
cmd.Parameters.AddRange(arr);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
this one on my Form
public static DataTable SP_SELECTALLCATEGORIES()
{
DataAccessLayer.Open();
DataTable dt = DataAccessLayer.ExecuteTable("SP_SELECTALLCATEGORIES", CommandType.StoredProcedure);
DataAccessLayer.Close();
return dt;
}
If my table consists of 1,000,000 records I want loading ProgressBar linked to the loaded state for example when application loading 500,000 the ProgressBar should be at 50%.
I think the reason because when we refer the data grid or binding to an element we only execute the SQL statement and it returns with the results so can we know how much of this result is loaded? thanks
Use a combination of the SQL COUNT() function and the DataTable.TableNewRow event. This event fires every time a new row is added to your table, which you can use to track progress.
First, get a row count.
SqlCommand countCmd = new SqlCommand(@"SELECT COUNT(column) FROM table;", cn);
int numRows = (int)countCmd.ExecuteScalar();
progressBar.Minimum = 0;
progressBar.Maximum = numRows;
Then, hook up the DataTable.TableNewRow event to track your progress.
tb.TableNewRow += (s,e) => progressBar.Value++;
da.Fill(tb);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With