简体   繁体   English

如何停止填充DataTable ASP.NET

[英]How to stop Filling DataTable ASP.NET

I didnt found any answer about posibility to stop Filling DataTable. 我没有找到有关停止填充DataTable的可能性的任何答案。 Is there any way to do this? 有什么办法吗? I have data from OleDbDataAdapter and sometime i have to wait 2 min but i want to say to the user to stop filling that amount of data or just just wait and continue. 我有来自OleDbDataAdapter的数据,有时我必须等待2分钟,但我想对用户说停止填充该数据量,或者只是等待并继续。

OleDbDataAdapter sqlArchiveData = new OleDbDataAdapter(sql_archive);

DataTable dt = new DataTable();

conn.Open();

sqlArchiveData.Fill(dt);

connProdcc1.Close();

Seems you can handle canceling the fill in the DataTable RowChanged event by passing a CancellationToken . 似乎可以通过传递CancellationToken DataTable RowChanged事件中的填充。 The following example spins up a Backgroundworker which isn't optimal in ASP.NET, but should give you some ideas. 下面的示例启动一个Backgroundworker ,它在ASP.NET中不是最佳选择,但应该可以给您一些想法。

Example taken from the following MSDN forum post. 示例取自以下MSDN论坛帖子。

https://social.msdn.microsoft.com/Forums/vstudio/en-US/ad4f5811-a08d-4342-bcf1-368ceac834fc/is-there-any-way-to-cancel-a-dataadapterfill-command-while-its-processing?forum=csharpgeneral https://social.msdn.microsoft.com/Forums/vstudio/en-US/ad4f5811-a08d-4342-bcf1-368ceac834fc/is-there-any-way-to-cancel-a-dataadapterfill-command-while-它的处理?论坛= csharpgeneral

private BackgroundWorker worker;
private DataTable table;

private void button2_Click(object sender, EventArgs e)
{
  if (worker != null)
  {
    worker.CancelAsync();
  }
}

private void button1_Click(object sender, EventArgs e)
{  
  this.worker = new BackgroundWorker();
  worker.WorkerReportsProgress = true;
  worker.WorkerSupportsCancellation = true;

  worker.DoWork += new DoWorkEventHandler(worker_DoWork);
  worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

  worker.RunWorkerAsync();
}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  MessageBox.Show(this.table.Rows.Count.ToString());
}

[System.Diagnostics.DebuggerStepThrough]
void worker_DoWork(object sender, DoWorkEventArgs e)
{
  this.table = new DataTable();

  using (SqlConnection connection= new SqlConnection())
  using (SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM table", connection))
  {
    table.RowChanged += new DataRowChangeEventHandler(table_RowChanged); 
    da.Fill(table);        
  }
}

[System.Diagnostics.DebuggerStepThrough]
void table_RowChanged(object sender, DataRowChangeEventArgs e)
{
  if (worker.CancellationPending)
  {
    throw new ApplicationException("Canceled"); // throw a spanner in the works
  }
  Thread.Sleep(5); // Just slow things down for testing
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM