I have a huge amount of data in MySQL exactly 10180 lines and its contionusly updating day by day with another 200-300 line. And in my C# app I have a backgroundworker which gets the data and loads into a datatable then this datatable loads into a ListView which shows the data.
The problem is this took approximately 1 minute which in production is too long.I want to fix that and optimize the code.
The code inside the DoWork method:
try
{
connection.Close();
if (lvValidate.InvokeRequired)
{
lvValidate.BeginInvoke(new MethodInvoker(delegate
{
lvValidate.Items.Clear();
}));
}
else
{
lvValidate.Items.Clear();
}
System.Data.DataTable dt = DataTransferDA("SELECT * FROM workhours ORDER BY ID desc");
foreach (System.Data.DataRow row in dt.Rows)
{
ListViewItem tempLv = new ListViewItem(row["ID"].ToString());
tempLv.SubItems.Add(row["Date"].ToString());
tempLv.SubItems.Add(row["Name"].ToString());
tempLv.SubItems.Add(row["WorkCode"].ToString());
tempLv.SubItems.Add(row["Assembly"].ToString());
tempLv.SubItems.Add(row["Tech"].ToString());
tempLv.SubItems.Add(row["Beginning"].ToString());
tempLv.SubItems.Add(row["Ending"].ToString());
tempLv.SubItems.Add(row["Validated"].ToString());
tempLv.SubItems.Add(row["Validated name"].ToString());
lvValidate.BeginInvoke(new MethodInvoker(delegate
{
lvValidate.Items.Add(tempLv);
}));
}
if (lvValidate.InvokeRequired)
{
lvValidate.BeginInvoke(new MethodInvoker(delegate
{
lvValidate.TopItem = lvValidate.Items[topIndex];
}));
}
else
{
lvValidate.TopItem = lvValidate.Items[topIndex];
}
connection.Close();
}
catch (Exception ex) { MessageBox.Show("Error happened: " + ex.Message); connection.Close();} }