C # WPF Create loading progress bar linked to collected results from database
I am developing C # WPF Simple Application. This application consists of multiple download frames depending on the database. For each table, I have one Frame
and they are all linked to the main page for navigation only. I have created a loading frame consisting of a simple progress bar.
This was an introduction now, here's my question.
I want to associate ProgressBar
with collecting results from a database. For example, this order: Select * from Tablename
I want to run ProgressBar
while the results are collected. I am using Query on my database, this is my method: this is 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 in 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 to load ProgressBar
related to the loaded state, for example when the app load 500,000 ProgressBar
should be at 50%.
I think the reason is that when we reference the data grid or element bindings, we only execute the SQL statement and return with the results so that we know how much of that result is loaded? thank
source to share
Use a combination of SQL function COUNT()
and event DataTable.TableNewRow
. This event fires every time a new row is added to your table, which you can use to track progress.
First get the number of lines.
SqlCommand countCmd = new SqlCommand(@"SELECT COUNT(column) FROM table;", cn);
int numRows = (int)countCmd.ExecuteScalar();
progressBar.Minimum = 0;
progressBar.Maximum = numRows;
Then hook up an event DataTable.TableNewRow
to track progress.
tb.TableNewRow += (s,e) => progressBar.Value++;
da.Fill(tb);
source to share