gpt4 book ai didi

c# - 如何停止填充数据表 ASP.NET

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:21 25 4
gpt4 key购买 nike

我没有找到任何关于停止填充数据表的可能性的答案。有什么办法吗?我有来自 OleDbDataAdapter 的数据,有时我必须等待 2 分钟,但我想告诉用户停止填充该数量的数据,或者只是等待并继续。

OleDbDataAdapter sqlArchiveData = new OleDbDataAdapter(sql_archive);

DataTable dt = new DataTable();

conn.Open();

sqlArchiveData.Fill(dt);

connProdcc1.Close();

最佳答案

似乎您可以取消 DataTable RowChanged event 中的填充通过传递 CancellationToken。以下示例启动一个 Backgroundworker这在 ASP.NET 中不是最优的,但应该给你一些想法。

示例取自以下 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

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
}

关于c# - 如何停止填充数据表 ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com