D
Domleg
Guest
Hello,
I have a button that will get results between 2 dates and should export them to Excel.
It works fine except the entire process takes 15 to 20 minutes for 20+ thousand rows where on in SQL Management Studio it takes 15 seconds.
What am I doing wrong in my code for this horrible performance?
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
dscmd.SelectCommand.CommandTimeout = 900;
DataSet ds = new DataSet();
dscmd.Fill(ds);
progressBar2.Maximum = ds.Tables[0].Rows.Count;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
progressBar2.Value = i;
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
if (i == 0)
{
data = ds.Tables[0].Columns[j].ColumnName.ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
else
{
data = ds.Tables[0].Rows.ItemArray[j].ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
}
}
xlWorkBook.SaveAs("c:\\Indications.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\Indications.xls");
progressBar2.Value = 0;
Continue reading...
I have a button that will get results between 2 dates and should export them to Excel.
It works fine except the entire process takes 15 to 20 minutes for 20+ thousand rows where on in SQL Management Studio it takes 15 seconds.
What am I doing wrong in my code for this horrible performance?
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
dscmd.SelectCommand.CommandTimeout = 900;
DataSet ds = new DataSet();
dscmd.Fill(ds);
progressBar2.Maximum = ds.Tables[0].Rows.Count;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
progressBar2.Value = i;
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
if (i == 0)
{
data = ds.Tables[0].Columns[j].ColumnName.ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
else
{
data = ds.Tables[0].Rows.ItemArray[j].ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
}
}
xlWorkBook.SaveAs("c:\\Indications.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\Indications.xls");
progressBar2.Value = 0;
Continue reading...