等待时间过长从DB取数据

本文关键字:数据 DB 等待时间 | 更新日期: 2023-09-27 18:03:49

虽然获取数据似乎很长,但我们不能在这段时间内执行其他任务。我使用BAckGroundWOrker用于此目的。但是,在获取所有数据后等待很长时间,只有应用程序运行良好

  private void btnExrtPDF_Click(object sender, RoutedEventArgs e)
     {
         btnExrtPDF.IsEnabled = false;
         Collection.Clear();                
         long NoOfRecords = 10000;
         long RecordsIcrease = 10000;
         SaveFileDialog xsfd = new SaveFileDialog()
         {
             FileName = "Book1",
             DefaultExt = ".xlsx",
             Filter = "Excel Document|*.xlsx",
             InitialDirectory = new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).ToString()
         };
         Nullable<bool> result = xsfd.ShowDialog();
         System.Data.DataTable batchFCSB = new System.Data.DataTable();
         int row = 0;
         if (result == true)
         {
             DetailReportFCBuySell = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy);
             batchFCSB = DetailReportFCBuySell.ToDataTable();
             Collection.Add(row, batchFCSB);
             row = 1;
             PageIndex++;
             for (long k = NoOfRecords; k < DetailReportFCBuySell.FirstOrDefault().TotalRecords; k = +NoOfRecords)
             {
                     new AlxServiceClient().Using(channel =>
                     {
                         DetailReportFCBuySell = new ObservableCollection<DLReports.FCBuySellDetail>();
                         DetailReportFCBuySell = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy);
                         batchFCSB = new System.Data.DataTable();
                         batchFCSB = DetailReportFCBuySell.ToDataTable();
                         Collection.Add(row, batchFCSB);
                         row++;   
                 });
                 NoOfRecords = NoOfRecords + RecordsIcrease;
             }

             for (int k = 0; k < Collection.Keys.Count; k++)
             {
                 string xlsxFile = string.Empty;
                 xlsxFile = System.IO.Path.GetTempFileName();                    
                 TableView temp = new TableView();
                 temp.DataContext = (Collection.Where(i => i.Key == k).FirstOrDefault().Value);
                // ExportToXlsx(temp, xlsxFile);
             }
            }
m_oWorker.RunWorkerAsync();
}

等待时间过长从DB取数据

我将editvalue设置为一个单独的对象,并在线程中调用该对象

DateTime startDate =    Convert.ToDateTime(dateEdtStartDate.EditValue).Date;
DateTime endDate = Convert.ToDateTime(dtpEditEndDate.EditValue).Date;
private DetailReportFCBuySell FetchRecord ()
{
return ObservableCollection<DLReports.FCBuySellDetail> temp = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, startDate, endDate, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy);
}

代码块似乎从click事件开始运行而不调用BGWorker,考虑将代码块移动到单个函数中,如

private DetailReportFCBuySell FetchRecord ()
{
    return AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy);
}

并在m_oWorker Do Work事件中调用此函数

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    FetchRecord();
}

阅读更多关于https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx#code-snippet-3